基本運動オブジェクト : ActionScript

Pocket

ActionScriptを使った基本的な運動をオブジェクトにまとめました。

ActionScriptコード

function MotionEffect(target) {
	// 操作対象
	var target = target;
	// 移動 : 移動スピード, 加速度, X軸移動距離,Y軸移動距離
	this.move = function(speed, speed_up, xDistance, yDistance) {
		var speed = speed;
		var speed_up = speed_up;
		var xDistance = xDistance;
		var yDistance = yDistance;
		var startX = target._x;
		var startY = target._x;
		target.onEnterFrame = function() {
			var currentXDistance = this._x-startX;
			var currentYDistance = this._y-startY;
			if (currentXDistance<xDistance) {
				speed += speed_up;
				this._x += speed;
			} else {
				this._x = startX+xDistance;
			}
			if (currentYDistance<yDistance) {
				speed += speed_up;
				this._y += speed;
			} else {
				this._y = startY+yDistance;
			}
		};
	};
	// 跳ね返り : x軸スピード,y軸スピード, 加速度, X軸移動距離, Y軸移動距離
	this.bounce = function(xSpeed, ySpeed, speed_up, xDistance, yDistance) {
		// speed_upが0以外では思う動きにならない。
		var xSpeed = xSpeed;
		var ySpeed = ySpeed;
		var speed_up = speed_up;
		var xDistance = xDistance;
		var yDistance = yDistance;
		var startX = target._x;
		var startY = target._x;
		target.onEnterFrame = function() {
			var currentXDistance = this._x-startX;
			var currentYDistance = this._y-startY;
			if (currentXDistance>xDistance) {
				this._x = startX+xDistance;
				xSpeed = -xSpeed;
			}
			if (currentXDistance<0) {
				this._x = startX;
				xSpeed = -xSpeed;
			}
			if (currentYDistance>yDistance) {
				this._y = startY+yDistance;
				ySpeed = -ySpeed;
			}
			if (currentYDistance<0) {
				this._y = startY;
				ySpeed = -ySpeed;
			}
			if (xDistance>0) {
				xSpeed += speed_up;
				this._x += xSpeed;
			}
			if (yDistance>0) {
				ySpeed += speed_up;
				this._y += ySpeed;
			}
		};
	};
	// 収束 : 収束先X, 収束先Y, 収束スピード, 誤差
	this.convergence = function(endX, endY, speed, margin) {
		// 目標位置
		var endX = endX;
		var endY = endY;
		// 収束スピード
		var speed = speed;
		// 誤差 収束と判定
		var margin = margin;
		// 収束処理
		target.onEnterFrame = function() {
			var diffX = (endX-this._x);
			var diffY = (endY-this._y);
			if (Math.abs(diffX)>margin && Math.abs(diffY)>margin) {
				trace(diffX);
				this._x += diffX/speed;
				this._y += diffY/speed;
			} else {
				this._x = endX;
				this._y = endY;
				delete this.onEnterFrame;
			}
		};
	};
	// 円運動 : 開始角度, 終了角度, 半径, 中心座標X,Y, 角度の増分
	this.circle = function(startDegree, endDegree, radius, startX, startY, inc) {
		// 現在の角度
		var degree = startDegree;
		// 終了角度
		var endDegree = endDegree;
		// 半径
		var radius = radius;
		// 円の中心
		var startX = startX;
		var startY = startY;
		// 角度の増分
		var inc = inc;
		// イベントハンドラ
		target.onEnterFrame = function() {
			if (degree>endDegree) {
				delete this.onEnterFrame;
			} else {
				// 弧度
				var theta = Math.PI/180*degree;
				// 位置
				this._x = startX+radius*Math.cos(theta);
				this._y = startY+radius*Math.sin(theta);
				degree += inc;
			}
		};
	};
	// sin関数 : 開始角度, 半径, 角度の増分
	this.sin = function(startDegree, endDegree, radius, inc) {
		// 現在角度
		var degree = startDegree;
		// 終了角度
		var endDegree = endDegree;
		//経
		var radius = radius;
		// 現在座標
		var startX = target._x;
		var startY = target._y;
		// イベントハンドラ
		target.onEnterFrame = function() {
			if (degree>endDegree) {
				delete this.onEnterFrame;
			} else {
				// 弧度の計算
				var theta = (Math.PI/180)*degree;
				// 座標の計算
				this._x = startX+radius*theta;
				this._y = startY+radius*Math.sin(theta);
				degree += inc;
			}
		};
	};
	// con関数 : 開始角度, 半径, 角度の増分
	this.cos = function(startDegree, endDegree, radius, inc) {
		// 現在角度
		var degree = startDegree;
		// 終了角度
		var endDegree = endDegree;
		//経
		var radius = radius;
		// 現在座標
		var startX = target._x;
		var startY = target._y;
		// イベントハンドラ
		target.onEnterFrame = function() {
			if (degree>endDegree) {
				delete this.onEnterFrame;
			} else {
				// 弧度の計算
				var theta = (Math.PI/180)*degree;
				// 座標の計算
				this._x = startX+radius*theta;
				this._y = startY+radius*Math.cos(theta);
				degree += inc;
			}
		};
	};
}

利用例

var iniObject = {_x:100, _y:100};
var target = attachMovie("clip", "clip", 0, iniObject);
var motionObject = new MotionEffect(target);
motionObject.circle(0, 3600, 50, 250, 250, 20);

コメント

No comments yet.

コメントの投稿

改行と段落タグは自動で挿入されます。
メールアドレスは表示されません。