2017-02-02 80 views
1

我這裏有這個AS3類檢測,如果鼠標移動:AS3和Flex 4 - 申請AS3類在Flex MXML文件

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

    } 
} 

我所試圖做的是應用這個類我的主MXML的Flex文件,所以當我的鼠標移動到我的項目中時,會調用mouseMoved方法。我將如何做到這一點?

回答

3

MXML文件已經是一個類,您可以爲它們添加腳本。你不能直接使用你的類,因爲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>