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


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

 

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(00, stage.stageWidth, stage.stageHeight);
               _canvas.graphics.endFill();
               
               _canvas.graphics.lineStyle(20x000000);
               
               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);
          }
          
     }

}

        



Leave a Comment / Note


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


Flash / Flex / ActionScript examples

 Navioo Development
» Drag Drop