forked from: forked from: #1 LikeATimeLine WonderflBook Interactive2
forked from forked from: #1 LikeATimeLine WonderflBook Interactive2 (diff: 66)
@author Takashi Murai(KAYAC)
ActionScript3 source code
/**
* Copyright mhayashi ( http://wonderfl.net/user/mhayashi )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/fKdq
*/
// forked from masahirohayashi's 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 WonderflBook extends Sprite {
private var _ball:Sprite;
private var _locus:Sprite;
private var _timeGage:Sprite;
private var _startX:Number=0;//xのスタート位置
private var _startY:Number=0;
private var _endX:Number=stage.stageWidth;//xのエンド位置
private var _frameCount:uint=0;//再生ヘッド(フレーム数カウント用の変数)
private var _frameCountLimit:uint=150;//コマ数(最終フレームの位置)
private var _animationFrames:Array;//フレーム格納用の配列
public function WonderflBook(){
init();
}
public function init():void{//初期化メソッド
_animationFrames=generate_animationFrames();//アニメーションフレームの生成
_ball=new Sprite();//このコードで唯一のグラフィックオブジェクト。画面に表示される_ballです。
_ball.graphics.lineStyle(1,0x00FFFF);
_ball.graphics.beginFill(0x00FFFF,0.2);
_ball.graphics.drawCircle(0,0,5);
_ball.graphics.endFill();
_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.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 generate_animationFrames():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;
// 点のインスタンスを配列にpush
tmp.push(new Point(tmpX, tmpY));//各コマのx座標, y座標をコマ数だけ計算して配列に追加
}
return tmp;
}
private function render(e:Event):void{//レンダリング用のメソッド
if(_frameCount < _frameCountLimit){//現在のフレーム数が_frameCountLimit以内なら
// 現在のフレームの座標を取得(Point)
_ball.x=_animationFrames[_frameCount].x;
_ball.y=_animationFrames[_frameCount].y;
}else{
//stop();//アニメーション終了
_frameCount = 0;
}
_timeGage.width=(_frameCount/_frameCountLimit)*stage.stageWidth;
_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, recordMouse);
};
private function stopRec(e:Event):void{
removeEventListener(Event.ENTER_FRAME, recordMouse);
};
// マウスの座標を記録
private function recordMouse(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);
}
}
}
}
