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

操舵力の実装(AIのフロッキングをする前に・・・)

操舵力を実装する。
@author SIBA
Get Adobe Flash player
by siba2260 23 Feb 2009
package
{
	import flash.display.Sprite;
	import flash.events.Event;
	
	[SWF(width=800, height=600, backgroundColor=0xAADDFF)]

	/**
	 * 操舵力を実装する。
	 * @author SIBA
	 */
	public class Main03 extends Sprite
	{
		// ----------------------------
		//	メンバ変数
		// ----------------------------
		
		private var vehicle:Vehicle;
		
		
		// ----------------------------
		//	初期化
		// ----------------------------
		
		public function Main03()
		{
			vehicle = new Vehicle();
			vehicle.x = 300;
			vehicle.y = 400;
			addChild(vehicle);
			
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		
		
		// ----------------------------
		//	イベント
		// ----------------------------
		
		private function onEnterFrame(event:Event):void
		{
			vehicle.move(mouseX, mouseY);
		}
		
	}
}


import flash.geom.Point;
import flash.display.Sprite;
import flash.display.Graphics;

class Vehicle extends Sprite
{
	// --------------------------------
	//	定数
	// --------------------------------
	
	private const MAX_TURN_THETA:uint = 10;
	private const MAX_FOURCE:uint = 10;
	
	
	// --------------------------------
	//	メンバ変数
	// --------------------------------
	
	private var velocity:Number = 0;
	
	
	// --------------------------------
	//	初期化
	// --------------------------------
	
	public function Vehicle():void
	{
		initDesign();
	}
	
	private function initDesign():void
	{
		const g:Graphics = graphics;
		g.beginFill(0xFF5555);
		g.moveTo(-25, -20);
		g.lineTo(25, 0);
		g.lineTo(-25, 20);
		g.lineTo(-5, 0);
		g.lineTo(-25, -20);
		g.endFill();
	}
	
	
	// --------------------------------
	//	パブリックメソッド
	// --------------------------------
	
	public function move(mouseX:Number, mouseY:Number):void
	{
		velocity = Point.distance(new Point(mouseX, mouseY), new Point(x, y));
		velocity /= 10;
		velocity = velocity < MAX_FOURCE? velocity : MAX_FOURCE;
		
		var tempRotation:Number = Math.atan2(mouseY - y, mouseX - x)/Math.PI * 180;
		tempRotation -= rotation;
		if (tempRotation > 180) tempRotation -= 360;
		if (tempRotation < -180) tempRotation += 360;
		tempRotation /= 10;
		if (Math.abs(tempRotation) >= MAX_TURN_THETA)
		{
			tempRotation = tempRotation < 0? -MAX_TURN_THETA : MAX_TURN_THETA;
		}
		
		rotation += tempRotation;
		x += velocity * Math.cos(rotation/180 * Math.PI);
		y += velocity * Math.sin(rotation/180 * Math.PI);
	}
	
}