In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

forked from: SimpleDragAndDrop2

SimpleDragにアニメーションをつけた
Get Adobe Flash player
by t2421 23 Mar 2011
/**
 * Copyright t2421 ( http://wonderfl.net/user/t2421 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/m7y5
 */

// forked from kamibana's SimpleDragAndDrop
package {
    import flash.events.*;
    import flash.display.Sprite;
    import flash.display.DisplayObject;
    public class SimpleDragAndDrop extends Sprite {
        private var area:MyRect;
        private var ddObj:MyRect;
        private var currentDrag:MyRect;
        private var easing:Number = 0.3;
        public function SimpleDragAndDrop() {
            // write as3 code here..
            area = new MyRect(100,50,0xff0000);
            area.x = stage.stageWidth/2;
            area.y = stage.stageHeight/2;
            ddObj = new MyRect();
            ddObj.tempX = ddObj.x;
            ddObj.tempY = ddObj.y;
            addChild(ddObj);
            addChild(area);
            startDD();
        }
        
        private function startDD():void{
            ddObj.addEventListener(MouseEvent.MOUSE_DOWN,downHandler);    
            addChild(ddObj);
            
        }
        
        private function downHandler(e:MouseEvent):void{
            MyRect(e.target).startDrag();
            currentDrag = MyRect(e.target);
            stage.addEventListener(MouseEvent.MOUSE_UP,upHandler);
            if(this.hasEventListener(Event.ENTER_FRAME)){
                this.removeEventListener(Event.ENTER_FRAME,loop);
            }

        }
        
        private function upHandler(e:MouseEvent):void{
            currentDrag.stopDrag();
            stage.removeEventListener(MouseEvent.MOUSE_UP,upHandler);
            checkHit(currentDrag,area);
        }
        
        private function checkHit(obj:DisplayObject,area:DisplayObject):void{
            if(obj.hitTestObject(area)){
                currentDrag.x = area.x;
                currentDrag.y = area.y;
            }else{
                
                this.addEventListener(Event.ENTER_FRAME,loop);
            }
        }
        
        private function loop(e:Event):void{
            var distX:Number = (currentDrag.x - currentDrag.tempX);
            var distY:Number = (currentDrag.y - currentDrag.tempY);
            currentDrag.x -= distX*easing;
            currentDrag.y -= distY*easing;
            if(distX < 1 || distY < 1){
                currentDrag.x = currentDrag.tempX;
                currentDrag.y = currentDrag.tempY;
                this.removeEventListener(Event.ENTER_FRAME,loop);
            }

            
        }


    }
}
import flash.display.Sprite;

class MyRect extends Sprite
{
    public var tempX:Number;
    public var tempY:Number;
    public function MyRect(_width:Number=100,_height:Number=50,_color:uint=0x000000){
        this.graphics.beginFill(_color);
        this.graphics.drawRect(0,0,_width,_height);
        this.graphics.endFill();
    }

}