RSS

Blog in Low-Room

2010
12/16

0

AS3.0復習18【ENTER_FRAME|イージング】

Category : AS/Flash, Web | Tagged :

インスタンスを移動させる際、移動距離に比例して加速/減速させるなどの表現(イージング)を、AS上でENTER_FRAMEを使って表現する事ができる。


import flash.events.MouseEvent;
import flash.events.Event;

var myBox:box = new box;
myBox.x = myBox.width/2;
myBox.y = stage.height/2;
myBox.buttonMode = true;

var myButton:button = new button;
myButton.x = stage.width/2;
myButton.y = stage.height - 30;
myButton.buttonMode = true;

addChild(myBox);
addChild(myButton);

function moveStart (e:MouseEvent):void {
	stage.addEventListener (Event.ENTER_FRAME, boxMove);
}

function boxMove (e:Event):void {
	//変数boxDistanceに、myBoxを移動させたい総距離の数値を代入
	var boxDistance = stage.width-myBox.width/2 - myBox.x;

	//myBox.xに1フレームごと移動する距離を代入
	//1フレームづつ、移動距離が2で割られる
	myBox.x = myBox.x + boxDistance/2;

	//処理が重くなるのを回避する為の記述
	//移動距離が1より小さくなったら、イベントリスナをremoveする
	if(boxDistance < 1){
		stage.removeEventListener (Event.ENTER_FRAME, boxMove);
	}
	//移動距離が1フレームづつ小さくなっている事が確認できる
	trace(Math.floor(boxDistance));
}

myButton.addEventListener (MouseEvent.CLICK, moveStart);

function boxReset (e:MouseEvent):void {
	myBox.x = myBox.width/2;
}

myBox.addEventListener (MouseEvent.CLICK, boxReset);

MOVEボタンをクリックすると移動開始します。四角をクリックすると、元の位置に戻ります。

This movie requires Flash Player 9


目標のx位置(変数boxDistance)が近づくにつれ、インスタンスの移動距離が減る。
1フレームづつ、現在のフレームのインスタンスのx位置(myBox.x)÷2で再生されるので、フレームが進むにつれ減速していく。



twitter

twitter

PAGE TOP

Proudly powered by WordPress.