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

[Progression版] ボタンでジャンプ + つかんでドラッグ

// forked from nacookan's ボタンでジャンプ + つかんでドラッグ
// forked from nacookan's つかんでドラッグしてスクロールするテスト
package{
    import flash.text.*;
    import flash.display.*;
    import flash.events.*;
    import flash.net.URLRequest;    
    import jp.progression.commands.*;
    import caurina.transitions.Tweener;

    [SWF(backgroundColor="0x000000", frameRate="30")]

    public class TweenScrollTest extends Sprite{
        public function TweenScrollTest():void{
            var image:Sprite = new Sprite();
            image.buttonMode = true;
            this.addChild(image);

            var url:String = 'http://farm1.static.flickr.com/67/199819013_42eca9cf82_o.jpg';
            var req:URLRequest = new URLRequest(url);
            var loader:Loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event):void{
                image.addChild(loader);

                var credit:TextField = new TextField();
                credit.htmlText = 'by <u><font color="#0000ff"><a href="http://www.flickr.com/photos/galaygobi/">galaygobi</a></font></u>';
                credit.autoSize = TextFieldAutoSize.LEFT;
                image.addChild(credit);

                generateButton(image, 100, 100, function():void{
                    jump(image, -1000, -500);
                });

                generateButton(image, 1300, 700, function():void{
                    jump(image, -100, -300);
                });

                generateButton(image, 200, 500, function():void{
                    jump(image, -800, 0);
                });

                generateButton(image, 1000, 200, function():void{
                    jump(image, -500, -600);
                });

                generateButton(image, 600, 900, function():void{
                    jump(image, 0, 0);
                });
            });
            loader.load(req);

            var startX:Number = 0;
            var startY:Number = 0;
            var imageX:Number = 0;
            var imageY:Number = 0;
            var lastX:Number = 0;
            var lastY:Number = 0;
            var lasttime:Number = 0;
            var drag:Boolean = false;
            image.addEventListener(MouseEvent.MOUSE_DOWN, function(e:MouseEvent):void{
                startX = e.stageX
                startY = e.stageY
                imageX = image.x;
                imageY = image.y;
                drag = true;
                // Progression の DoTweener#execute() で動かしてるのに、
                // キャンセルするときは Tweener を直接使うのがイマイチだなあ。
                Tweener.removeAllTweens();
            });
            image.addEventListener(MouseEvent.MOUSE_MOVE, function(e:MouseEvent):void{
                if(!drag) return;
                image.x = imageX + (e.stageX - startX);
                image.y = imageY + (e.stageY - startY);
                
                var t:Number = (new Date()).getTime();
                if(t - lasttime < 200) return;
                lastX = e.stageX;
                lastY = e.stageY;
                lasttime = t;
            });
            image.addEventListener(MouseEvent.MOUSE_UP, function(e:MouseEvent):void{
                drag = false;

                var now:Number = (new Date()).getTime();
                if(200 < now - lasttime) return;

                (new DoTweener(image, {
                    time: (now - lasttime) / 1000 * 20,
                    x: (image.x + (e.stageX - lastX)),
                    y: (image.y + (e.stageY - lastY)),
                    transition: 'easeOutExpo'
                })).execute();
            });
        }

        private function generateButton(parent:Sprite, x:Number, y:Number, handler:Function):Sprite{
            var button:Sprite = new Sprite();
            button.graphics.lineStyle(2, 0xa0a0a0);
            button.graphics.beginFill(0xe0e0e0);
            button.graphics.drawRect(0, 0, 100, 30);
            button.graphics.endFill();
            button.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void{
                handler();
            });
            button.x = x;
            button.y = y;
            parent.addChild(button);
            return button;
        }

        private function jump(target:Sprite, x:Number, y:Number):void{
            var plist:ParallelList = new ParallelList();
            plist.addCommand(
                new DoTweener(target, {
                    time: 2,
                    x: x,
                    y: y,
                    transition: 'easeInOutExpo'
                }), [
                    new DoTweener(target, {
                        time: 1,
                        z: 500,
                        transition: 'easeOutExpo'
                    }),
                    new DoTweener(target, {
                        time: 1,
                        z: 0,
                        transition: 'easeInOutElastic'
                    })
                ]
            );
            plist.execute();
        }
    }
}