简体   繁体   English

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

[英]AS3 and Flex 4 - Applying AS3 Class to Flex mxml file

I have this AS3 Class here which detects if a mouse has moved: 我这里有这个AS3类,可以检测鼠标是否移动了:

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")
        }

    }
}

What I am trying to do is apply this class my main mxml Flex file so when my mouse moves in my project the mouseMoved method is called. 我要做的是将此类应用于我的主要mxml Flex文件,这样当我的鼠标在我的项目中移动时,将调用mouseMoved方法。 How would I do this? 我该怎么做?

The MXML file is already a class, you can add script to them. MXML文件已经是一个类,您可以向它们添加脚本。 You cannot use your class directly because MXML use flex architecture and MXML Component need to extends UIComponent, not Sprite. 您不能直接使用您的类,因为MXML使用flex体系结构,而MXML Component需要扩展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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM