2

编辑 2: Pastebin 链接到完整的 ball、bat 和 AIbat 类。

编辑:我已经更新了 turbo 代码,只是对它进行了更好的评论。我还有一个指向我在 Pastebin 中的所有 GameplayScreen 代码的链接,因为我不想用它来淹没这个页面。

我正在做一个乒乓球类型的游戏。我正在创建一个通电,它会导致三件事发生:

1)球减速(工作) 2)屏幕变成黑白(工作) 3 蝙蝠减速(不工作

我只是使用“速度”变量调整球的速度,一切正常。我正在尝试对球棒做同样的事情,但在比赛过程中速度没有变化。然而,当我调试时,在激活电源后,我可以将鼠标悬停在速度变量上,并看到它已从默认速度7.0f 更改30.0f,但游戏本身没有发生明显的变化。

我还在屏幕上运行调试,显示球棒和球的速度,我注意到球的速度会相应变化,他的右球棒(AiBat)也是如此。它移动到 30。

但出于某种原因,左球棒(玩家控制的球棒)保持相同的速度。奇怪的。

我在这里做错了什么,但我一生都无法弄清楚。为什么我可以在一个位置更改速度并且它工作正常,但在另一个位置却不行?

Class bat
{

        /// <summary>
    /// Controls the bat moving up the screen
    /// </summary>
    public void MoveUp()
    {
        SetPosition(Position + new Vector2(0, -moveSpeed * elapsedTime));

    }

        /// <summary>
    /// Updates the position of the AI bat, in order to track the ball
    /// </summary>
    public virtual void UpdatePosition(Ball ball, GameTime gameTime)
    {
        size.X = (int)Position.X;
        size.Y = (int)Position.Y;

        elapsedTime = 50.0f * (float)gameTime.ElapsedGameTime.TotalSeconds;

        // Just here for debugging. Hitting Z WORKS FINE and slows the bats down
        previous = current;
        current = Keyboard.GetState();
        if (current.IsKeyDown(Keys.Z))
        {
            elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds * 5.0f;
        }
    }
 }

Class GameplayScreen
{

  .......
  private int disableCooldown;
  private int coolDown;
  private int powerDisableCooldown = 2000;
  private int powerEnableCooldown = 5000;
  ......


        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)

                // Updating bat position
                leftBat.UpdatePosition(ball, gameTime);
                rightBat.UpdatePosition(ball, gameTime);

                if (gScaleActivated == true)
                {
                    leftBat.moveSpeed = 30.0f;
                    rightBat.moveSpeed = 30.0f;
                }


                // If spaceBar is down and the turbo bar is not empty, activate turbo. If not, turbo remains off 
                if (input.ActivateTurbo(ControllingPlayer))
                {
                    if (disableCooldown > 0)
                    {
                        leftBat.isTurbo = true;
                        coolDown = powerEnableCooldown;
                        leftBat.moveSpeed = 30.0f;
                        disableCooldown -= gameTime.ElapsedGameTime.Milliseconds;
                    }
                    else
                    {   
                        leftBat.DisableTurbo();
                    }
                }

                    // If spacebar is not down, begin to refill the turbo bar
                else
                {
                    leftBat.DisableTurbo();
                    coolDown -= gameTime.ElapsedGameTime.Milliseconds;
                    // If the coolDown timer is not in effect, then the bat can use Turbo again
                    if (coolDown < 0)
                    {
                        disableCooldown = powerDisableCooldown;
                    }
                }

                // Makes sure that if Turbo is on, it is killd it after () seconds
                if (leftBat.isTurbo)
                {
                    disableCooldown -= gameTime.ElapsedGameTime.Milliseconds;
                }

    /// <summary>
    /// Logic to trigger Powerups
    /// </summary>
    private void PowerupActivated(object sender, PowerupEventArgs e)
    {
       ...........
                           case PowerupType.SlowMo:
                    {
                        this.SlowMo(gScaleActivated);
                        gScaleActivated = true;
                        break;
                    }
       ...........
    }


    // Activates the SlowMo and grayscale effect for the powerup.
    public void SlowMo(bool gScaleActivated)
    {
                    gScaleActivated = true;
                    ball.SlowMoBall();
                    AudioManager.Instance.PlaySoundEffect("SlowMotion1");
    }
}
4

2 回答 2

0

在没有看到所有类的情况下,一个猜测是,因为您在 Update 中设置 moveSpeed 之前执行了 UpdatePositions 调用,所以左侧球棒的 moveSpeed 进一步向下重置,因此当您在每个周期执行 UpdatePosition 调用时,它会短暂地保持在其重置值。要对此进行测试,请尝试在更新位置之前设置 moveSpeed?

            leftBat.UpdatePosition(ball, gameTime);
            rightBat.UpdatePosition(ball, gameTime);

            if (gScaleActivated == true)
            {
                leftBat.moveSpeed = 30.0f;
                rightBat.moveSpeed = 30.0f;
            }
于 2012-07-02T10:47:40.840 回答
0

嗯,这很奇怪。我解决了。我只是注释掉了我的 Turbo 代码。我不确定为什么它与 slowMo 有任何关系,因为它仅在我按空格键时才会激活。

                    #region Turbo stuff
                // If spaceBar is down and the turbo bar is not empty, activate turbo. If not, turbo remains off 
                if (input.ActivateTurbo(ControllingPlayer))
                {
                    if (disableCooldown > 0)
                    {
                        leftBat.isTurbo = true;
                        coolDown = powerEnableCooldown;
                        leftBat.moveSpeed = 30.0f;
                        disableCooldown -= gameTime.ElapsedGameTime.Milliseconds;
                    }
                    else
                    {
                        leftBat.DisableTurbo();
                    }
                }

                    // If spacebar is not down, begin to refill the turbo bar
                else
                {
                    leftBat.DisableTurbo();
                    coolDown -= gameTime.ElapsedGameTime.Milliseconds;
                    // If the coolDown timer is not in effect, then the bat can use Turbo again
                    if (coolDown < 0)
                    {
                        disableCooldown = powerDisableCooldown;
                    }
                }

                // Makes sure that if Turbo is on, it is killd it after () seconds
                if (leftBat.isTurbo)
                {
                    disableCooldown -= gameTime.ElapsedGameTime.Milliseconds;
                }

                if (disableCooldown < 0)
                {
                    leftBat.isTurbo = false;
                }

                #endregion 
于 2012-07-03T10:53:16.360 回答