AS3 和 Flex 4 - 将 AS3 Class 应用于 Flex mxml 文件

AS3 and Flex 4 - Applying AS3 Class to Flex mxml file

我这里有这个 AS3 Class,它可以检测鼠标是否移动过:

package
{
    import flash.display.Sprite;
    import flash.events.MouseEvent;

    public class ApplicationTimer extends Sprite
    { 

        public function ApplicationTimer()
        {   
            stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoved);
        }

        public function mouseMoved(event:MouseEvent):void 
        { 
            trace("mouse moved")
        }

    }
}

我想做的是应用此 class 我的主要 mxml Flex 文件,因此当我的鼠标在我的项目中移动时,将调用 mouseMoved 方法。我该怎么做?

MXML 文件已经是class,您可以向其中添加脚本。您不能直接使用 class,因为 MXML 使用 flex 架构并且 MXML 组件需要扩展 UIComponent,而不是 Sprite。

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
               mouseMove="mouseMoveHandler(event)">

    <fx:Script>
        <![CDATA[
            protected function mouseMoveHandler(event:MouseEvent):void
            {
                trace(event);
            }
        ]]>
    </fx:Script>

    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
</s:Application>