操舵力の実装(AIのフロッキングをする前に・・・) 操舵力を実装する。 @author SIBA siba2260 forked:0favorite:4lines:64license : All rights reserved modified : 2009-02-24 02:59:32 Embed Tweet 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); } } Code Fullscreen Preview Fullscreen NaoHachi kiriswonderf.. : animationgame paq : move blackwater : 鼠标跟随 animation game lineTo Point.distance Math.PI Math.atan2 Math.abs endFill beginFill Math.cos moveTo Point Math.sin Event.ENTER_FRAME uint Event Number