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

Home
Flash / Flex / ActionScript
1.Animation
2.Array
3.Class
4.Data Type
5.Development
6.Function
7.Graphics
8.Language
9.Network
10.Regular Expressions
11.Statement
12.String
13.TextField
14.XML
Flash / Flex / ActionScript » 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);
          }
          
     }

}

        
Related examples in the same category
1.Dragging and Dropping Objects with the Mouse
2.Dragging and Dropping
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.