010310

by Nowloading_ forked from #4 forked from: #3 MouseRecord forked from: #2 Loop forked from: #1 LikeATimeLine WonderflBook (diff: 115)
@author Takashi Murai(KAYAC)
♥0 | Line 86 | Modified 2010-08-19 10:43:18 | MIT License
play

ActionScript3 source code

/**
 * Copyright Nowloading_ ( http://wonderfl.net/user/Nowloading_ )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/sxqZ
 */

// forked from Murai's #4 forked from: #3 MouseRecord forked from: #2 Loop forked from: #1 LikeATimeLine WonderflBook
// forked from Murai's #3 MouseRecord forked from: #2 Loop forked from: #1 LikeATimeLine WonderflBook Interactive2
// forked from Murai's #2 Loop forked from: #1 LikeATimeLine WonderflBook Interactive2
// forked from Murai's #1 LikeATimeLine WonderflBook Interactive2
package {
    
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.filters.GlowFilter;
    import flash.geom.Point;
    
    /*
    @author Takashi Murai(KAYAC)
    */
    
    [SWF(width="465",height="465",backgroundColor="0x000000",frameRate="30")]
    public class WonderflBook4 extends Sprite {
        
        private var ball:Sprite;
        private var timeGage:Sprite;
        private var locus:Sprite;
        
        private var startX:Number=0;//xのスタート位置
        private var endX:Number=stage.stageWidth;//xのエンド位置
        private var startY:Number=stage.stageHeight/2;//yのスタート位置
        private var frameCount:uint=0;//再生ヘッド(フレーム数カウント用の変数)
        private var frameCountLimit:uint=150;//コマ数(最終フレームの位置)
        private var animationFrames:Array;//フレーム格納用の配列
        
        public function WonderflBook4(){
            init();
        }
        
        public function init():void{//初期化メソッド
            animationFrames=generateAnimationFrames();//アニメーションフレームの生成
            
            ball=new Sprite();//画面に表示されるball用Sprite
            ball.graphics.lineStyle(1,0x00FFFF);
            ball.graphics.beginFill(0x00FFFF,0.2);
            ball.graphics.drawCircle(0,0,5);
            ball.graphics.endFill();
            ball.x=startX;
            ball.y=startY;
            
            timeGage=new Sprite();//画面下部のゲージ用Sprite
            timeGage.graphics.beginFill(0xFFFFFF,0.5);
            timeGage.graphics.drawRect(0,0,stage.stageWidth,3);
            timeGage.graphics.endFill();
            timeGage.x=-1;
            timeGage.y=stage.stageHeight-3;
            
            locus=new Sprite();//軌跡表示用Sprite
            
            //ball.blendMode=BlendMode.ADD;
            ball.filters=[new GlowFilter(0x00FFFF,1,16,16,2,2)];
            
            addChild(locus);
            addChild(timeGage);
            addChild(ball);//ballをDisplayTreeへ登録
            
            stage.addEventListener(MouseEvent.MOUSE_DOWN,startRec);
            stage.addEventListener(MouseEvent.MOUSE_UP,stopRec);
            
            updateLocus();//軌跡の初期化
            
            start();//レンダリング開始
        }
        
        private function generateAnimationFrames():Array{//アニメーションフレームを生成するメソッド
            var tmp:Array=new Array();//出力用の一時的な配列
            var easeRatio:Number=0.1;//イージングの比率
            var tmpX:Number=startX;
            var tmpY:Number=startY;
            for(var i:uint=0;i<frameCountLimit;i++){
                tmpX+=(endX-tmpX)*easeRatio;
                tmp.push(new Point(tmpX,tmpY));//各コマのx座標,y座標をコマ数だけ計算して配列に追加
            }
            return tmp;
        }
        
        private function render(e:Event):void{//レンダリング用のメソッド
            //現在のフレーム数がframeCountLimit以内なら
            if(frameCount < frameCountLimit){
                //今のコマ数のx座標に追従
                ball.x+=(animationFrames[frameCount].x-ball.x)/4;
                //今のコマ数のy座標に追従
                ball.y+=(animationFrames[frameCount].y-ball.y)/4;
            }else{
                //stop();//アニメーション終了
                frameCount=0;
            }
            //画面下部のゲージを連動させる
            timeGage.width=(frameCount/frameCountLimit)*stage.stageHeight;
            frameCount++;//再生ヘッドを進める
        }        
        
        public function start():void{addEventListener(Event.ENTER_FRAME,render);};//スタート用のメソッド。renderがENTER_FRAMEのタイミングで実行されるように設定
        public function stop():void{removeEventListener(Event.ENTER_FRAME,render);};//ストップ用のメソッド。renderがENTER_FRAMEのタイミングで実行されるように設定されているのを解除
        
        private function startRec(e:Event):void{addEventListener(Event.ENTER_FRAME,recMouse);};//記録スタート用のメソッド。
        private function stopRec(e:Event):void{removeEventListener(Event.ENTER_FRAME,recMouse);};//記録ストップ用のメソッド。
        
        private function recMouse(e:Event):void{//マウス記録中に呼び出されるメソッド
            if(frameCount < frameCountLimit){//フレームが指定フレーム以内じゃないと動作させない
                animationFrames[frameCount].x=mouseX;
                animationFrames[frameCount].y=mouseY;
                updateLocus();//軌跡を再描画
            }
        }
        
        private function updateLocus():void{//フレーム毎に記録されている座標から軌跡を描画するメソッド
            locus.graphics.clear();
            locus.graphics.lineStyle(1,0xFFFFFF,0.2);
            for(var i:uint=0;i<frameCountLimit;i++){
                locus.graphics.drawCircle(animationFrames[i].x,animationFrames[i].y,2);
            }
        }
    }
}