ActionScript3:我应该使用什么代码来阻止玩家控制的精灵移动?

ActionScript3: What code should I use to stop the player controlled sprite from moving?

我是 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;

我同意您的代码中存在一点逻辑问题。你的船会继续前进直到 审判日,因为两个变量决定了它的运动速度 - shipMoveX 和 shipMoveY - 不会随时间自动降级。

现在有数以千计的方法可以实现这一目标,但让我们保持现状 简单的。 您正在使用一个名为 thrustPower 的 class 变量 - 确保将其设置为 0.1 并且 shipMoveX & shipMoveY0。 另外添加这些 class 变量:

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;
}

这里我们正在处理这种情况,如果玩家既没有按下也没有按下并相乘 宇宙飞船 horizontal/vertical 速度 衰减 。如果你向后滚动一点,你会注意到 它的值为 0.97。 你一般可以说,如果你将一个正数 x 乘以 0 < y < 1,x 会变小。

所以如果你是 spaship 当前正在以每帧 3 个像素的速度水平移动 (shipMoveX=3 ; shipMoveY=0) 下一帧会变成2.91。下一帧它将是 2.8227,然后是 2.738019 ... 等等,直到它最终在无穷大中达到零。