Drag and drop a Sprite : Drag Drop « Development « Flash / Flex / ActionScript






Drag and drop a Sprite

 
package {
     
     import flash.display.Sprite;
     import flash.events.MouseEvent;
     
     [SWF(width=550, height=400)]

     public class Main extends Sprite {
          
          private var _canvas:Sprite = new Sprite();
          
          public function Main() {
               _canvas
               
               _canvas.graphics.beginFill(0xF0F0F0);
               _canvas.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
               _canvas.graphics.endFill();
               
               _canvas.graphics.lineStyle(2, 0x000000);
               
               addChild(_canvas);
               
               _canvas.addEventListener(MouseEvent.MOUSE_DOWN, onCanvasMouseDown);
               _canvas.addEventListener(MouseEvent.MOUSE_UP, onCanvasMouseUp);
          }
          
          private function onCanvasMouseDown(event:MouseEvent):void {
               _canvas.graphics.moveTo(event.localX, event.localY);
               _canvas.addEventListener(MouseEvent.MOUSE_MOVE, onCanvasMouseMove);
          }
          
          private function onCanvasMouseMove(event:MouseEvent):void {
               _canvas.graphics.lineTo(event.localX, event.localY);
               event.updateAfterEvent();
          }
          
          private function onCanvasMouseUp(event:MouseEvent):void {
               _canvas.graphics.lineTo(event.localX, event.localY);
               _canvas.removeEventListener(MouseEvent.MOUSE_MOVE, onCanvasMouseMove);
          }
          
     }

}

        








Related examples in the same category

1.Dragging and Dropping Objects with the Mouse
2.Dragging and Dropping