Flash / Flex / ActionScript/Development/MouseEvent

Материал из Web эксперт
Перейти к: навигация, поиск

A custom mouse pointer

 
package {
  import flash.display.*;
  import flash.ui.*;
  import flash.events.*;
  import flash.geom.*;
  public class CustomMousePointer extends Sprite {
    public function CustomMousePointer (  ) {
      graphics.lineStyle(1);
      graphics.beginFill(0x0000FF, 1);
      graphics.lineTo(15, 5);
      graphics.lineTo(5, 15);
      graphics.lineTo(0, 0);
      graphics.endFill(  );
      var stageDetector:StageDetector = new StageDetector(this);
      stageDetector.addEventListener(StageDetector.ADDED_TO_STAGE,
                                     addedToStageListener);
      stageDetector.addEventListener(StageDetector.REMOVED_FROM_STAGE,
                                     removedFromStageListener);
    }
    private function addedToStageListener (e:Event):void {
      Mouse.hide(  );
      stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveListener);
      stage.addEventListener(Event.MOUSE_LEAVE, mouseLeaveListener);
    }
    private function removedFromStageListener (e:Event):void {
      Mouse.show(  );
      stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveListener);
      stage.removeEventListener(Event.MOUSE_LEAVE, mouseLeaveListener);
    }
    private function mouseLeaveListener (e:Event):void {
      visible = false;
    }
    private function mouseMoveListener (e:MouseEvent):void {
      var pointInParent:Point = parent.globalToLocal(new Point(e.stageX,
                                                               e.stageY));
      x = pointInParent.x;
      y = pointInParent.y;
      e.updateAfterEvent(  );
      if (!visible) {
        visible = true;
      }
    }
  }
}



Add mouse listener to state

 
package {
  import flash.display.*;
  import flash.events.*;
  public class GetChildAtExample extends Sprite {
    public function GetChildAtExample(  ) {
   
      var color:Array = [ 0xFF0000, 0x990000, 0x660000, 0x00FF00,
                          0x009900, 0x006600, 0x0000FF, 0x000099,
                          0x000066, 0xCCCCCC ];
   
      for ( var i:int = 0; i < 10; i++ ) {
        var circle:Shape = createCircle( color[i], 10 );
        circle.x = i;
        circle.y = i + 10; // the + 10 adds padding from the top
        
        addChild( circle );
      }      
      
      stage.addEventListener( MouseEvent.CLICK, updateDisplay );
    }
   
    public function updateDisplay( event:MouseEvent ):void {
      setChildIndex( getChildAt(0), numChildren - 1 );
    }
    
    public function createCircle( color:uint, radius:Number ):Shape {
      var shape:Shape = new Shape(  );
      shape.graphics.beginFill( color );
      shape.graphics.drawCircle( 0, 0, radius );
      shape.graphics.endFill(  );
      return shape;
    }
  }
}



Add mouse move listener

 
package {
  import flash.display.Sprite;
  import flash.events.*;
  import flash.geom.Point;
  public class Main extends Sprite {
    public function Main(  ) {
      var circle:Sprite = new Sprite(  );
      circle.x = 10;
      circle.y = 10;
      circle.graphics.beginFill( 0xFF0000 );
      circle.graphics.drawCircle( 0, 0, 5 );
      circle.graphics.endFill(  );
      
      circle.addEventListener( MouseEvent.MOUSE_MOVE, handleMouseMove );
      addChild( circle );
    }
    
    private function handleMouseMove( event:MouseEvent ):void {
      trace( "local x: " + event.localX );
      trace( "local y: " + event.localY );
      
      var localPoint:Point = new Point( event.localX, event.localY );
      var globalPoint:Point = event.target.localToGlobal( localPoint );
      
      trace( "global x: " + globalPoint.x );
      trace( "global y: " + globalPoint.y );
    }
  }
}



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;
    }
    
  }
}



All types of mouse event

 
MouseEventExample
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
public class Main extends Sprite
{
    public function Main ()
    {
        var listener:Sprite = new Sprite();
        listener.graphics.beginFill(0x0000ff, 1);
        listener.graphics.drawRect(0, 0, 200, 200);
        listener.graphics.endFill();
        listener.doubleClickEnabled = true;
        addChild(listener);
        listener.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler);
        listener.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
        listener.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
        listener.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
        listener.addEventListener(MouseEvent.MOUSE_WHEEL, mouseWheelHandler);
        listener.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
        listener.addEventListener(MouseEvent.DOUBLE_CLICK, doubleClickHandler);
    }
    private function mouseDownHandler(mouseEvent:MouseEvent):void
    {
        trace("mouseDownHandler");
    }
    private function mouseMoveHandler(mouseEvent:MouseEvent):void
    {
        trace("mouseMoveHandler");
    }
    private function mouseOutHandler(mouseEvent:MouseEvent):void
    {
        trace("mouseOutHandler");
    }
    private function mouseUpHandler(mouseEvent:MouseEvent):void
    {
        trace("mouseUpHandler");
    }
    private function mouseWheelHandler(mouseEvent:MouseEvent):void
    {
        trace("mouseWheelHandler");
    }
    private function mouseOverHandler(mouseEvent:MouseEvent):void
    {
        trace("mouseOverHandler");
    }
    private function doubleClickHandler(mouseEvent:MouseEvent):void
    {
        trace("doubleClickHandler");
    }
}
}



Click Test

 
package {
     import flash.display.Sprite;
     import flash.events.MouseEvent;
     
     [SWF(width=550, height=400)]
     
     public class Main extends Sprite {
          
          public function Main() {
               var square:Sprite = new Sprite();
               square.graphics.lineStyle(2, 0x000000);
               square.graphics.beginFill(0xff0000);
               square.graphics.drawRect(0, 0, 100, 100);
               square.graphics.endFill();
               addChild(square);
               
               square.doubleClickEnabled = true;
               
               square.addEventListener(MouseEvent.CLICK, onSquareClick);
               square.addEventListener(MouseEvent.DOUBLE_CLICK, onSquareDoubleClick);
          }
          
          private function onSquareClick(event:MouseEvent):void {
               trace("ouch!");
          }
          
          private function onSquareDoubleClick(event:MouseEvent):void {
               trace("double ouch!");
          }
     
     }
}



Creating Mouse Interactions

 
package {
  import flash.display.Sprite;
  import flash.events.*;
  import flash.geom.Point;
  public class Main extends Sprite {
    public function Main(  ) {
      var circle:Sprite = new Sprite(  );
      circle.x = 10;
      circle.y = 10;
      circle.graphics.beginFill( 0xFF0000 );
      circle.graphics.drawCircle( 0, 0, 5 );
      circle.graphics.endFill(  );
      
      circle.addEventListener( MouseEvent.MOUSE_MOVE, handleMouseMove );
      addChild( circle );
    }
    
    private function handleMouseMove( event:MouseEvent ):void {
      trace( "mouse move" );
    }
  }
}



Finding the mouse pointer"s position

 
package {
  import flash.display.*;
  import flash.events.*;
  import flash.text.*;
  public class Main extends Sprite {
    public function Main (  ) {
      var textfield:TextField = new TextField(  );
      textfield.text = "Click here";
      textfield.autoSize = TextFieldAutoSize.LEFT;
      textfield.x = 100;
      textfield.y = 100;
      stage.addChild(textfield);
      textfield.addEventListener(MouseEvent.CLICK, clickListener);
    }
    private function clickListener (e:MouseEvent):void {
      trace("Position in TextField"s coordinate space: ("+ e.localX + ", " + e.localY + ")");
      trace("Position in Stage instance"s coordinate space: ("+ e.stageX + ", " + e.stageY + ")");
    }
  }
}



Global Mouse Down Sensor

 
package {
  import flash.display.*;
  import flash.events.*;
  public class Main extends Sprite {
    public function Main (  ) {
      stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownListener);
    }
    private function mouseDownListener (e:MouseEvent):void {
      trace("The primary mouse button was pressed.");
    }
  }
}



Handling Mouse Events "Globally"

 
package {
  import flash.display.*;
  import flash.events.*;
  public class Main extends Sprite {
    public function Main(  ) {
      stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveListener);
    }
    private function mouseMoveListener (e:MouseEvent):void {
      trace("The mouse moved.");
    }
  }
}



mouseEnabled = false

 
package {
  import flash.display.*;
  import flash.events.*;
  import flash.text.*;
  public class Main extends Sprite {
    public function Main (  ) {
      var circle:Sprite = new Sprite(  );
      circle.graphics.beginFill(0x999999, 1);
      circle.graphics.lineStyle(1);
      circle.graphics.drawEllipse(0, 0, 100, 100);
      var textfield:TextField = new TextField(  );
      textfield.text = "Click here";
      textfield.autoSize = TextFieldAutoSize.LEFT;
      textfield.x = 30;
      textfield.y = 30;
      textfield.border = true;
      textfield.background = true;
      textfield.mouseEnabled = false;
      
      addChild(circle);
      addChild(textfield);
      circle.addEventListener(MouseEvent.CLICK, clickListener);
    }
    private function clickListener (e:MouseEvent):void {
      trace("User clicked: " + e.target);
      DisplayObject(e.target).x += 10;
    }
  }
}



Mouse Event Hierarchy

 
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
public class Main extends Sprite
{
    private var firstListener:Sprite = new Sprite();
    private var secondListener:Sprite = new Sprite();
    public function Main(){
        firstListener.graphics.beginFill(0x0000ff, 1);
        firstListener.graphics.drawCircle(0, 0, 100);
        firstListener.graphics.endFill();
        secondListener.graphics.beginFill(0x00ff00, 1);
        secondListener.graphics.drawCircle(0, 0, 30);
        secondListener.graphics.endFill();
        addChild(firstListener);
        addChild(secondListener);
        firstListener.y = 300;
        secondListener.y = 250;
        this.stage.addEventListener(MouseEvent.MOUSE_DOWN, mainMouseDown);
        firstListener.addEventListener(MouseEvent.MOUSE_DOWN, firstMouseDown);
        secondListener.addEventListener(MouseEvent.MOUSE_DOWN, secondMouseDown);
        this.stage.addEventListener(MouseEvent.MOUSE_MOVE, mainMouseMove);
        firstListener.addEventListener(MouseEvent.MOUSE_MOVE, firstMouseMove);
        secondListener.addEventListener(MouseEvent.MOUSE_MOVE, secondMouseMove);
    }
    private function mainMouseMove(me:MouseEvent):void
    {
        trace(" stage move ");
    }
    private function firstMouseMove(me:MouseEvent):void
    {
        trace("first move ");
    }
    private function secondMouseMove(me:MouseEvent):void
    {
        trace("second move");
    }
    private function mainMouseDown(me:MouseEvent):void
    {
        trace(" stage down ");
    }
    private function firstMouseDown(me:MouseEvent):void
    {
        trace("first down ");
    }
    private function secondMouseDown(me:MouseEvent):void
    {
        trace("second down ");
    }
}
}



Mouse Events and Modifier Keys

 
package {
  import flash.display.*;
  import flash.events.*;
  public class Main extends Sprite {
    public function Main() {
      stage.addEventListener(MouseEvent.CLICK, clickListener);
    }
    private function clickListener (e:MouseEvent):void {
      if (e.shiftKey) {
        trace("Shift+click detected");
      }
    }
  }
}



Mouse Events and Overlapping Display Objects

 
package {
  import flash.display.*;
  import flash.events.*;
  import flash.text.*;
  public class Main extends Sprite {
    public function Main (  ) {
      var circle:Sprite = new Sprite(  );
      circle.graphics.beginFill(0x999999, 1);
      circle.graphics.lineStyle(1);
      circle.graphics.drawEllipse(0, 0, 100, 100);
      var textfield:TextField = new TextField(  );
      textfield.text = "Click here";
      textfield.autoSize = TextFieldAutoSize.LEFT;
      textfield.x = 30;
      textfield.y = 30;
      textfield.border = true;
      textfield.background = true;
      addChild(circle);
      addChild(textfield);
      circle.addEventListener(MouseEvent.CLICK, clickListener);
    }
    private function clickListener (e:MouseEvent):void {
      trace("User clicked: " + e.target);
      DisplayObject(e.target).x += 10;
    }
  }
}



Post-Event Screen Updates

 
package{
    import flash.display.GradientType;
    import flash.display.Sprite;
    import flash.geom.*;
    import flash.events.*;
    import flash.ui.*;
    public class Main extends Sprite {
    
         public function Main() {
            var triangle:Sprite = new Sprite(  );
            triangle.graphics.lineStyle(1);
            triangle.graphics.beginFill(0x00FF00, 1);
            triangle.graphics.moveTo(25, 0);
            triangle.graphics.lineTo(50, 25);
            triangle.graphics.lineTo(0, 25);
            triangle.graphics.lineTo(25, 0);
            triangle.graphics.endFill(  );
            triangle.x = 200;
            triangle.y = 100;
            
            triangle.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveListener);
            
         }
        private function mouseMoveListener (e:MouseEvent):void {
          var pointInParent:Point = parent.globalToLocal(new Point(e.stageX,e.stageY));
          x = pointInParent.x;
          y = pointInParent.y;
        
          e.updateAfterEvent(  );
        
          if (!visible) {
            visible = true;
          }
        }
    }
}



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



Rollover Test

 
package {
     
     import flash.display.Sprite;
     import flash.events.MouseEvent;
     
     [SWF(width=550, height=400)]
     public class Main extends Sprite {
     
          private static var UP_COLOR:uint = 0xFF0000;
          private static var ROLLOVER_COLOR:uint = 0x0000FF;
          
          private var _square:Sprite = new Sprite();
          
          public function Main() {
               drawSquare(UP_COLOR);
               addChild(_square);
               
               _square.doubleClickEnabled = true;
               
               _square.addEventListener(MouseEvent.CLICK, onSquareClick);
               _square.addEventListener(MouseEvent.DOUBLE_CLICK, onSquareDoubleClick);
               _square.addEventListener(MouseEvent.ROLL_OVER, onSquareRollOver);
               _square.addEventListener(MouseEvent.ROLL_OUT, onSquareRollOut); 
          }
          
          private function drawSquare(color:uint):void {
               _square.graphics.clear();
               _square.graphics.lineStyle(2, 0x000000);
               _square.graphics.beginFill(color);
               _square.graphics.drawRect(0, 0, 100, 100);
               _square.graphics.endFill();
          }
          
          private function onSquareClick(event:MouseEvent):void {
               trace("ouch!");
          }
          
          private function onSquareDoubleClick(event:MouseEvent):void {
               trace("double ouch!");
          }
          
          private function onSquareRollOver(event:MouseEvent):void {
               drawSquare(ROLLOVER_COLOR);
          }
          
          private function onSquareRollOut(event:MouseEvent):void {
               drawSquare(UP_COLOR);
          }
     }
}



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();
        }
    }
}