Using MouseEvent in Conjunction with the Mouse : MouseEvent « Development « Flash / Flex / ActionScript






Using MouseEvent in Conjunction with the Mouse

 
package
{
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.ui.Mouse;
    public class Main extends Sprite
    {
        private var circleMouse:Sprite = new Sprite();
        private var squareMouse:Sprite = new Sprite();
        private var currentMouse:Sprite = new Sprite();

        private var useSquareField:Sprite = new Sprite();
        private var useCircleField:Sprite = new Sprite();

        public function Main()
        {
            useSquareField.graphics.beginFill(0xFFFFFF, 1);
            useSquareField.graphics.drawRect(0, 0, 100, 100);
            useSquareField.graphics.endFill();
            useSquareField.name = "square";
            useSquareField.x = 200;

            useCircleField.graphics.beginFill(0xFFFFFF, 1);
            useCircleField.graphics.drawRect(0, 0, 100, 100);
            useCircleField.name = "circle";
            useCircleField.graphics.endFill();
            addChild(useCircleField);
            addChild(useSquareField);

            useCircleField.addEventListener(MouseEvent.ROLL_OVER, useCircle);
            useSquareField.addEventListener(MouseEvent.ROLL_OVER, useSquare);
            useCircleField.addEventListener(MouseEvent.ROLL_OUT, showMouse);
            useSquareField.addEventListener(MouseEvent.ROLL_OUT, showMouse);

            circleMouse.graphics.beginFill(0x00ff00, 1);
            circleMouse.graphics.drawCircle(0, 0, 5);
            circleMouse.graphics.endFill();
            currentMouse = new Sprite();

            squareMouse.graphics.beginFill(0xff0000, 1);
            squareMouse.graphics.drawRect(0, 0, 10, 10);
            squareMouse.graphics.endFill();
            this.stage.addEventListener(MouseEvent.MOUSE_MOVE, moveNewMouse);
        }

        private function moveNewMouse(mouseEvent:MouseEvent):void

        {
            currentMouse.x = mouseEvent.stageX + 5;
            currentMouse.y = mouseEvent.stageY + 5;
            mouseEvent.updateAfterEvent();
        }

        private function useSquare(mouseEvent:MouseEvent):void
        {
            Mouse.hide();
            addChild(squareMouse);
            currentMouse = squareMouse;
        }

        private function useCircle(mouseEvent:MouseEvent):void
        {
            Mouse.hide();
            addChild(circleMouse);
            currentMouse = circleMouse;
        }

        private function showMouse(mouseEvent:MouseEvent):void
        {
            if((mouseEvent.target as Sprite) == useSquareField)
            {
                removeChild(squareMouse);
            }
            else {
                removeChild(circleMouse);
            }
            currentMouse = new Sprite();
            Mouse.show();
        }
    }
}

        








Related examples in the same category

1.Responding to Mouse and Key Events
2.Mouse Events and Overlapping Display Objects
3.mouseEnabled = false
4.Finding the mouse pointer's position
5.A custom mouse pointer
6.Handling Mouse Events "Globally"
7.Global Mouse Down Sensor
8.Mouse Events and Modifier Keys
9.Mouse Event Hierarchy
10.All types of mouse event
11.Add mouse listener to state
12.Creating Mouse Interactions
13.Add mouse move listener
14.Add mouse up and down listener
15.Rollover Test
16.Click Test
17.Post-Event Screen Updates