Add mouse up and down listener : MouseEvent « Development « Flash / Flex / ActionScript






Add mouse up and down listener

 
package {
  import flash.display.Sprite;
  import flash.events.MouseEvent;
  public class Main extends Sprite {
    
    private var isDrawing:Boolean;
    
    public function Main(  ) {
      graphics.lineStyle( 20, 0xFFCC33 );
      
      isDrawing = false;
      
      stage.addEventListener( MouseEvent.MOUSE_DOWN, startDrawing );
      stage.addEventListener( MouseEvent.MOUSE_MOVE, draw );
      stage.addEventListener( MouseEvent.MOUSE_UP, stopDrawing );
    }
    
    public function startDrawing( event:MouseEvent ):void {
      graphics.moveTo( mouseX, mouseY );
      isDrawing = true;
    }
    
    public function draw( event:MouseEvent ):void {
      if ( isDrawing ) {
        graphics.lineTo( mouseX, mouseY );  
      }
    }
    
    public function stopDrawing( event:MouseEvent ):void {
      isDrawing = false;
    }
    
  }
}

        








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.Using MouseEvent in Conjunction with the Mouse
12.Add mouse listener to state
13.Creating Mouse Interactions
14.Add mouse move listener
15.Rollover Test
16.Click Test
17.Post-Event Screen Updates