好的,所以我有一个太空射击游戏,从上面看。当用鼠标左右移动时,船向上移动(-y)并稍微旋转。最大旋转在-10到10之间,宽度为* 0.5。 为了让“子弹”朝着船所面向的方向射击,我正在尝试使用cos sin公式。只要船的旋转为-5或5(-10 * 0.5,10 * 0.5),它就能很好地工作,但是一旦旋转接近0,就会开始在整个地方开火:)
我已经读过我需要将度数转换为弧度以便能够使用cos和sin,并且我已经尝试过 - 我得到的最好结果是它只向左或向右发射,具体取决于旋转,没有角度变化。
现在我是AS的新手,所以为可理解的代码道歉:)
任何人都可以帮我弄清楚为什么角度在-5和5时工作正常,但是当它在0时关闭时会变得疯狂吗?
谢谢你们!
import flash.events.KeyboardEvent;
import flash.events.Event;
var spaceship_mc:spaceship = new spaceship();
stage.addChild(spaceship_mc);
spaceship_mc.x = stage.stageWidth / 2;
spaceship_mc.y = stage.stageHeight / 2;
stage.addEventListener(Event.ENTER_FRAME,shipMovement);
function shipMovement(event:Event):void
{
var dx:int = spaceship_mc.x - mouseX;
var dy:int = spaceship_mc.y - mouseY;
spaceship_mc.x -= dx / 15;
spaceship_mc.y -= dy /20;
if (dx >= -10 && dx <= 0)
{
spaceship_mc.rotation = -dx * 0.5;
}
else if (dx >= 0 && dx <= 10)
{
spaceship_mc.rotation = dx * -0.5;
}
else if (dx > -10)
{
spaceship_mc.rotation = -10 * 0.5;
}
else if (dx < 10)
{
spaceship_mc.rotation = 10 * 0.5;
}
if (spaceship_mc.rotation > 0)
{
spaceship_mc.scaleX = 1-(0.05 * spaceship_mc.rotation);
}
if (spaceship_mc.rotation < 0)
{
spaceship_mc.scaleX = 1-(0.05 * -(spaceship_mc.rotation));
}
if (dy >= 0 && dy < 22 )
{
spaceship_mc.afterburner_mc.y = 22;
}
else if (dy >= 22 && dy <= 32)
{
spaceship_mc.afterburner_mc.y = dy;
}
else if (dy > 32)
{
spaceship_mc.afterburner_mc.y = 32;
}
else if (dy < 0)
{
spaceship_mc.afterburner_mc.y = 22;
}
var spaceshipHalfWidth:uint = spaceship_mc.width/2;
var spaceshipHalfHeight:uint = spaceship_mc.height/2;
if (spaceship_mc.x + spaceshipHalfWidth > stage.stageWidth)
{
spaceship_mc.x = stage.stageWidth - spaceshipHalfWidth;
}
else if (spaceship_mc.x - spaceshipHalfWidth < 0)
{
spaceship_mc.x = 0 + spaceshipHalfWidth;
}
if (spaceship_mc.y - spaceshipHalfHeight < 0)
{
spaceship_mc.y = 0 + spaceshipHalfHeight;
}
else if (spaceship_mc.y + spaceshipHalfHeight > stage.stageHeight)
{
spaceship_mc.y = stage.stageHeight - spaceshipHalfHeight;
}
}
addEventListener(MouseEvent.CLICK, skyt);
function skyt(e:MouseEvent):void
{
nyttSkudd();
}
function nyttSkudd()
{
var leftPhoton_mc:photon = new photon();
var rightPhoton_mc:photon = new photon();
leftPhoton_mc.x = spaceship_mc.x - 25;
leftPhoton_mc.y = spaceship_mc.y - 10;
rightPhoton_mc.x = spaceship_mc.x + 16;
rightPhoton_mc.y = spaceship_mc.y - 10;
leftPhoton_mc.rotation = spaceship_mc.rotation;
rightPhoton_mc.rotation = spaceship_mc.rotation;
addChild(leftPhoton_mc);
addChild(rightPhoton_mc);
addChild(spaceship_mc);
leftPhoton_mc.addEventListener(Event.ENTER_FRAME, skudd);
rightPhoton_mc.addEventListener(Event.ENTER_FRAME, skudd);
}
function skudd(e:Event):void
{
var photonAngle = e.target.rotation;
var photonSpeed = 10;
if (photonAngle == 0)
{
e.target.y -= photonSpeed;
}
else if (photonAngle > 0)
{
e.target.x += photonSpeed * Math.cos(photonAngle);
e.target.y += photonSpeed * Math.sin(photonAngle);
}
else if (photonAngle < 0)
{
e.target.x -= photonSpeed * Math.cos(photonAngle);
e.target.y -= photonSpeed * Math.sin(photonAngle);
}
if (e.target.y < 0 || e.target.y > 400)
{
e.target.removeEventListener(Event.ENTER_FRAME, skudd);
e.target.parent.removeChild(e.target);
}
}
答案 0 :(得分:2)
photonAngle是度,但Math.sin / cos需要弧度。所以你需要进行以下转换。
var photonAngle = e.target.rotation * Math.PI / 180.0;
也
e.target.x += photonSpeed * Math.cos(photonAngle);
e.target.y += photonSpeed * Math.sin(photonAngle);
就足够了,你不需要if / else语句:
if (photonAngle == 0)
{
e.target.y -= photonSpeed;
}
else if (photonAngle > 0)
{
e.target.x += photonSpeed * Math.cos(photonAngle);
e.target.y += photonSpeed * Math.sin(photonAngle);
}
else if (photonAngle < 0)
{
e.target.x -= photonSpeed * Math.cos(photonAngle);
e.target.y -= photonSpeed * Math.sin(photonAngle);
}
祝你好运!