Dragging and Dropping : Drag Drop : Development Flash / Flex / ActionScript examples


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

 

Dragging and Dropping


 
package{
import flash.display.Sprite;
import flash.events.MouseEvent;
public class Main extends Sprite
{
    private var draggableSprite:Sprite = new Sprite();
    private var droppableSprite:Sprite = new Sprite();
    private var nonDroppableSprite:Sprite = new Sprite();

    public function Main ()
    {
        draggableSprite.graphics.beginFill(0xff00001);
        draggableSprite.graphics.drawCircle(0010);
        draggableSprite.graphics.endFill();

        droppableSprite.graphics.beginFill(0x0000ff1);
        droppableSprite.graphics.drawRect(00100100);
        droppableSprite.graphics.endFill();

        nonDroppableSprite.graphics.beginFill(0xff00001);
        nonDroppableSprite.graphics.drawRect(00100100);
        nonDroppableSprite.graphics.endFill();
        addChild(nonDroppableSprite);
        addChild(droppableSprite);
        addChild(draggableSprite);

        nonDroppableSprite.x = 200;
        droppableSprite.x = 500;
        nonDroppableSprite.y = 100;
        droppableSprite.y = 100;
        draggableSprite.y = 100;

        draggableSprite.addEventListener(MouseEvent.MOUSE_DOWN, 
            startDragSprite);
        draggableSprite.addEventListener(MouseEvent.MOUSE_UP, 
            stopDragSprite);
    }

    private function startDragSprite(mouseEvent:MouseEvent):void
    {
        (mouseEvent.target as Sprite).startDrag();
    }

    private function stopDragSprite(mouseEvent:MouseEvent):void
    {
        var target:Sprite = mouseEvent.target as Sprite;
        if (target.dropTarget == droppableSprite)
        {
            target.stopDrag();
        else {
            target.x = 0;
        }
    }
}
}

        



Leave a Comment / Note


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


Flash / Flex / ActionScript examples

 Navioo Development
» Drag Drop