我目前遇到了关于对象朝向另一个对象旋转的问题。目前情况如图所示,我的对象在原点时能够旋转并移动到3个象限中除了正90度到180度象限的对象。当移动到该对象时,我的对象会旋转几圈一段时间。不知道有没有人知道为什么?我用的旋转语句是 rotation += (foodLocationDegrees - (rotation - 90)) * .2;
其中food是用来计算的。
var angle:Number = Math.atan2(foodTarget.y - y, foodTarget.x - x);
foodLocationDegrees =Math.floor((angle * 180 / Math.PI));
y和x是一个鱼的对象,foodtarget是食物的对象。
public function moveToFood():void
{
var dx:Number = x - _destinationX;
var dy:Number = y - _destinationY;
trace("Food location degree relative to fish mouth "+foodLocationDegrees);
var targetRotation:Number = 0;
if (foodLocationDegrees > 0 && foodLocationDegrees < 90)
{
trace("food is on 1st quadrant of the fish mount");
this.x -= dx / 18;
this.y -= dy / 18;
}else if (foodLocationDegrees > 90 && foodLocationDegrees < 180)
{
trace("food is on 2nd quadrant of the fish mount");
this.x -= dx / 18;
this.y -= dy / 18;
}else if (foodLocationDegrees > -180 && foodLocationDegrees < -90)
{
trace("food is on 3nd quadrant of the fish mount");
this.x -= dx / 18;
this.y -= dy / 18;
}else if (foodLocationDegrees < 0 && foodLocationDegrees > -90)
{
trace("food is on 4nd quadrant of the fish mount");
this.x -= dx / 18;
this.y -= dy / 18;
}
trace("Before adding Rotation " + rotation);
var number:Number = (foodLocationDegrees - (rotation - 90)) * .1;
trace("rotation to add "+number);
rotation += (foodLocationDegrees - (rotation - 90)) * .2;
trace("After adding Rotation " + rotation);
//removing food when both hit boxes hit
if (hit.hitTestObject(foodTarget.hit))
{
foodInPond--;
foodTarget.removeSelf();
}
}