Dragging and Dropping Objects with the Mouse : Drag Drop : Development Flash / Flex / ActionScript examples


Flash / Flex / ActionScript examples » Development » Drag Drop »

 

Dragging and Dropping Objects with the Mouse


 
package {
  import flash.display.Sprite;
  import flash.display.DisplayObject;
  import flash.events.MouseEvent;
  import flash.geom.Point;
  import flash.filters.DropShadowFilter;

  public class Main extends Sprite {
    
    private var _red:Sprite;
    private var _green:Sprite;
    private var _blue:Sprite;
    private var _white:Sprite;
    
    private var startingLocation:Point;
    
    public function Main(  ) {
      _red = new Sprite(  );
      _red.graphics.beginFill0xFF0000 );
      _red.graphics.drawRect0101010 );
      _red.graphics.endFill(  );
      
      _green = new Sprite(  )
      _green.graphics.beginFill0x00FF00 );
      _green.graphics.drawRect0301010 );
      _green.graphics.endFill(  );
      
      _blue = new Sprite(  );
      _blue.graphics.beginFill0x0000FF );
      _blue.graphics.drawRect0501010 );
      _blue.graphics.endFill(  );
      
      _white = new Sprite(  );
      _white.graphics.beginFill0xFFFFFF );
      _white.graphics.drawRect20105050 );
      _white.graphics.endFill(  );
      
      addChild_red );
      addChild_green );
      addChild_blue );
      addChild_white );  

      _red.addEventListenerMouseEvent.MOUSE_DOWN, pickup );
      _red.addEventListenerMouseEvent.MOUSE_UP, place );
      
      _green.addEventListenerMouseEvent.MOUSE_DOWN, pickup );
      _green.addEventListenerMouseEvent.MOUSE_UP, place );
      
      _blue.addEventListenerMouseEvent.MOUSE_DOWN, pickup );
      _blue.addEventListenerMouseEvent.MOUSE_UP, place );
    }
    
    public function pickupevent:MouseEvent ):void {
      startingLocation = new Point(  );
      startingLocation.x = event.target.x;
      startingLocation.y = event.target.y;
      
      event.target.startDrag(  );
      event.target.filters = new DropShadowFilter(  ) ];
      
      setChildIndexDisplayObjectevent.target ), numChildren - );
    }
    
    public function placeevent:MouseEvent ):void {
      event.target.stopDrag(  );
      event.target.filters = null;
      
      if event.target.dropTarget == _white ) {
        var color:uint;
        switch event.target ) {
          case _red: color = 0xFF0000break;
          case _green: color = 0x00FF00break;
          case _blue: color = 0x0000FFbreak;
        }
        
        _white.graphics.clear(  );
        _white.graphics.beginFillcolor );
        _white.graphics.drawRect20105050 );
        _white.graphics.endFill(  );
      }
      
      event.target.x = startingLocation.x;
      event.target.y = startingLocation.y;
    }
    
  }
}

        



Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .


Flash / Flex / ActionScript examples

 Navioo Development
» Drag Drop