在运行期间尝试调整球棒(球拍,乒乓球)的速度

时间:2012-06-28 16:12:55

标签: c# xna

编辑2: Pastebin link to the full ball, bat, and AIbat classes.

编辑:我已经更新了turbo代码,并且稍微评论了一下。我也有一个指向我所有GameplayScreen code in Pastebin的链接,因为我不想用它来填充这个页面。

我正在打乒乓球比赛。我正在创建一个能够导致三件事发生的通电:

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

2 个答案:

答案 0 :(得分:0)

在没有看到所有类的情况下,一个猜测是因为你在Update中设置moveSpeed之前进行了UpdatePositions调用,左边的bat会让它的moveSpeed进一步向下重置,所以当你每次执行UpdatePosition调用时,它会暂时保持其重置值周期。要测试这个,尝试在更新位置之前设置moveSpeed?

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

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

答案 1 :(得分: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