我是ActionScript3的新手,正在制作小行星类游戏。现在,当您松开移动按钮时,船将继续沿直线漂浮,我希望能够阻止这种情况的发生。我在考虑或者是专用的制动按钮(例如b键),或者如果没有按下该键以停止运动,则以较容易的方式进行。就像我说的那样,我真的是AS3的新手,所以甚至不确定我的代码的哪一部分正在使它们保持直线飞行。这是控制运动的代码供参考:
// register key presses
public function keyDownFunction(event:KeyboardEvent) {
if (event.keyCode == 37) {
leftArrow = true;
} else if (event.keyCode == 39) {
rightArrow = true;
} else if (event.keyCode == 38) {
upArrow = true;
//Add event listener for down arrow
} else if (event.keyCode == 40) {
downArrow = true;
// show thruster
if (gameMode == "play") ship.gotoAndStop(2);
} else if (event.keyCode == 32) { // space
var channel:SoundChannel = shootSound.play();
newMissile();
} else if (event.keyCode == 90) { // z
startShield(false);
var channel:SoundChannel = shieldSound.play();
}
}
// register key ups
public function keyUpFunction(event:KeyboardEvent) {
if (event.keyCode == 37) {
leftArrow = false;
} else if (event.keyCode == 39) {
rightArrow = false;
} else if (event.keyCode == 38) {
upArrow = false;
//Add listener for down arrow
} else if (event.keyCode == 40) {
downArrow = false;
// remove thruster
if (gameMode == "play") ship.gotoAndStop(1);
}
}
// animate ship
public function moveShip(timeDiff:uint) {
// rotate and thrust
if (leftArrow) {
ship.rotation -= shipRotationSpeed*timeDiff;
} else if (rightArrow) {
ship.rotation += shipRotationSpeed*timeDiff;
} else if (upArrow) {
shipMoveX += Math.cos(Math.PI*ship.rotation/180)*thrustPower;
shipMoveY += Math.sin(Math.PI*ship.rotation/180)*thrustPower;
//Added down arrow movement to allow player to move backwards
} else if (downArrow) {
shipMoveX -= Math.cos(Math.PI*ship.rotation/180)*thrustPower;
shipMoveY -= Math.sin(Math.PI*ship.rotation/180)*thrustPower;
}
// move
ship.x += shipMoveX;
ship.y += shipMoveY;
答案 0 :(得分:0)
我同意您的代码中存在一些逻辑问题。您的船将继续行驶直到 审判日,因为两个变量决定了它的运动速度-shipMoveX 和shipMoveY-不会随着时间的推移而自动降级。
现在实际上有成千上万种方法可以实现,但是让我们保持现状 简单。 您正在使用名为 thrustPower 的类变量-确保将其设置为 0.1 ,并且确保 shipMoveX 和 shipMoveY < / strong>为 0 。 另外添加以下类变量:
private var thrustHorizontal:Number = 0;
private var thrustVertical:Number = 0;
private var speed:Number = 0;
private var maxSpeed:Number = 5;
private var decay:Number = 0.97;
用以下方法替换moveShip函数:
public function moveShip(timeDiff:uint):void
{
thrustHorizontal = Math.sin(Math.PI * ship.rotation / 180);
thrustVertical = Math.cos(Math.PI * ship.rotation / 180);
// rotate and thrust
if (leftArrow)
{
ship.rotation -= shipRotationSpeed * timeDiff;
}
else if (rightArrow)
{
ship.rotation += shipRotationSpeed * timeDiff;
}
else if (upArrow)
{
shipMoveX += thrustPower * thrustHorizontal;
shipMoveY += thrustPower * thrustVertical;
}
else if (downArrow)
{
shipMoveX -= thrustPower * thrustHorizontal;
shipMoveY -= thrustPower * thrustVertical;
}
if (!upArrow && !downArrow)
{
shipMoveX *= decay;
shipMoveY *= decay;
}
speed = Math.sqrt((shipMoveX * shipMoveX) + (shipMoveY * shipMoveY));
if (speed > maxSpeed)
{
shipMoveX *= maxSpeed / speed;
shipMoveY *= maxSpeed / speed;
}
ship.x += shipMoveX;
ship.y -= shipMoveY;
}
如您所见,有一个新的if块:
if (!upArrow && !downArrow)
{
shipMoveX *= decay;
shipMoveY *= decay;
}
如果玩家既不向上也不向下并乘以
衰变使飞船的水平/垂直速度。如果向后滚动一点,您会注意到
它的值为 0.97 。
通常可以说,如果将正数x乘以0 因此,如果您是Spaship,则当前以每帧3像素的水平移动(shipMoveX = 3; shipMoveY = 0)
下一帧将变为2.91。下一个帧将是2.8227,然后是2.738019 ...依此类推,直到它最终达到无穷大零为止。