我正在尝试在AS3中制造一枚寻的导弹。当你希望它完全按照一个点完成时很容易 - 但是如果目的地(目标)绕过它,我试图让火箭必须转身。
这就是我所拥有的 - 它几乎可以工作,但你可以看到它非常小巧(特别是如果鼠标是火箭的左侧)(如果它向右移动似乎工作正常)。
http://projectavian.com/rocket.swf
这是我的Rocket课程:
package
{
import flash.display.Sprite;
import flash.geom.Point;
import flash.events.Event;
public class Rocket extends Sprite
{
// vars
public var target:Point;
private const _vel:Number = 5;
private const _steer:Number = Math.PI / 36;
private var _destAng:Number = 0;
private var _ang:Number = 0;
private var _xv:Number = 0;
private var _yv:Number = 0;
/**
* Constructor
*/
public function Rocket()
{
// temp graphics
graphics.lineStyle(2, 0);
graphics.lineTo(26, 0);
addEventListener(Event.ENTER_FRAME, _handle);
}
/**
* Called on dispatch of Event.ENTER_FRAME
*/
private function _handle(e:Event):void
{
_destAng = _getAng();
_xv = Math.cos(_ang) * _vel;
_yv = Math.sin(_ang) * _vel;
x += _xv;
y += _yv;
if(_ang > _destAng) _ang -= _steer;
if(_ang < _destAng) _ang += _steer;
rotation = _ang * 180 / Math.PI;
}
/**
* Get the angle to target
*/
private function _getAng():Number
{
if(target)
{
var yp:Number = target.y - y;
var xp:Number = target.x - x;
return Math.atan2(yp, xp);
}
return 0;
}
}
}
我猜这个逻辑并不适用于导致导弹向左移动的任何东西:
if(_ang > _destAng) _ang -= _steer;
if(_ang < _destAng) _ang += _steer;
这也是我用来创建Rocket的代码,如果它有用的话:
var point:Point = new Point();
var rocket:Rocket = new Rocket();
rocket.target = point;
addChild(rocket);
addEventListener(Event.ENTER_FRAME, _handle);
function _handle(e:Event):void
{
point.x = mouseX;
point.y = mouseY;
}
答案 0 :(得分:3)
这应该适合你。
public class Rocket extends Sprite
{
// vars
public var target:Point;
private const _vel:Number = 5;
private const _steer:Number = Math.PI / 36;
private var _destAng:Number = 0;
private var _ang:Number = 0;
private var _xv:Number = 0;
private var _yv:Number = 0;
public const SPEED:uint = 50;
// TODO: get this from fixed timespet
public const TIME_DELTA:Number = 0.05;
public const RAD:Number = 57.2957795;
public const DEG:Number = 0.0174532925;
public function Rocket()
{
// temp graphics
graphics.lineStyle(2, 0);
// body should follow head
graphics.moveTo(-26, 0);
graphics.lineTo(0, 0);
addEventListener(Event.ENTER_FRAME, _handle);
}
/**
* Called on dispatch of Event.ENTER_FRAME
*/
private function _handle(e:Event):void
{
var a:Number = _turnToFace(new Point(x, y), target, this.rotation * Math.PI / 180, 0.1);
var dirX:Number = Math.cos(a);
var dirY:Number = Math.sin(a);
x += (dirX) * SPEED * TIME_DELTA;
y += (dirY) * SPEED * TIME_DELTA;
rotation = a * RAD;
}
private function _turnToFace(p:Point, f:Point, ca:Number, turnSpeed:Number):Number
{
var xp:Number = f.x - p.x;
var yp:Number = f.y - p.y;
var desiredAngle:Number = Math.atan2(yp, xp);
var difference:Number = _wrapAngle(desiredAngle - ca);
difference = Math.max(-turnSpeed, Math.min(turnSpeed, difference));
return _wrapAngle(ca + difference);
}
private function _wrapAngle(radians:Number):Number
{
var pi2:Number = Math.PI * 2;
while(radians < -Math.PI) radians += pi2;
while(radians > Math.PI) radians -= pi2;
return radians;
}
}
答案 1 :(得分:1)
我找到了这段代码here
Missile_speed = 20;
Missile_turning = 6;
DX = Target._x-Missile._x; DY = Target._y-Missile._y;
距离= Math.sqrt((DX * DX)+(DY * DY));
DX / =距离; DY / =距离;
VX + = DX * Missile_turning; VY + = DY * Missile_turning;
速度= Math.sqrt((VX * VX)+(VY * VY));
if(velocity&gt; Missile_speed) { VX =(VX * Missile_speed)/速度; VY =(VY * Missile_speed)/速度; }
Missile._x + = VX; Missile._y + = VY;
稍微调整一下(调用函数,基本的东西),它应该是你需要的。