1
votes

I have this AS3 Class here which detects if a mouse has moved:

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. How would I do this?

1

1 Answers

3
votes

The MXML file is already a class, you can add script to them. You cannot use your class directly because MXML use flex architecture and MXML Component need to extends UIComponent, not 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>