Responding to Mouse and Key Events : MouseEvent « Development « Flash / Flex / ActionScript






Responding to Mouse and Key Events

 

package {
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    
    public class Main extends Sprite {
        private var _sprite:Sprite;
    
        public function Main(  ) {
            _sprite = new Sprite(  );
            addChild(_sprite);
            _sprite.graphics.beginFill(0xffffff);
            _sprite.graphics.drawRect(0, 0, 400, 400);
            _sprite.graphics.endFill(  );

            _sprite.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
            _sprite.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
        }
        private function onMouseDown(event:MouseEvent):void {
            _sprite.graphics.lineStyle(1, 0, 1);
            _sprite.graphics.moveTo(mouseX, mouseY);
            _sprite.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
        }
        
        private function onMouseUp(event:MouseEvent):void
        {
            _sprite.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
        }

        private function onMouseMove(event:MouseEvent):void {
            _sprite.graphics.lineTo(mouseX, mouseY);
        }
    }
}

        








Related examples in the same category

1.Mouse Events and Overlapping Display Objects
2.mouseEnabled = false
3.Finding the mouse pointer's position
4.A custom mouse pointer
5.Handling Mouse Events "Globally"
6.Global Mouse Down Sensor
7.Mouse Events and Modifier Keys
8.Mouse Event Hierarchy
9.All types of mouse event
10.Using MouseEvent in Conjunction with the Mouse
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