首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >试着在运行时调整蝙蝠(球拍)的速度。

试着在运行时调整蝙蝠(球拍)的速度。
EN

Stack Overflow用户
提问于 2012-06-28 16:12:55
回答 2查看 473关注 0票数 2

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

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

我在做一个乒乓球式的游戏。我正在创建一个powerup,它会导致三种情况发生:

1)球慢下来(working) 2)屏幕变黑变白(working) 3只蝙蝠减速(not working)

我只是用“速度”变量来调整球的速度,一切都很好。我也在试着对蝙蝠做同样的事情,但是在比赛中速度并没有改变。但是,当我调试时,在激活powerup之后,我可以在速度变量上悬停,并看到它已经从它的默认速度7.0f更改为30.0f,但是在游戏本身中没有发生明显的更改。

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

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

我在这里做了些错事,但我无法为自己的生命找到答案。为什么我可以改变速度在一个地方,它工作很好,但在另一个地方不行?

代码语言:javascript
复制
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;
        }
    }
 }
代码语言:javascript
复制
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");
    }
}
EN

回答 2

Stack Overflow用户

发布于 2012-07-02 10:47:40

在没有看到所有类的情况下,一种猜测是,由于您在更新中设置UpdatePositions之前执行了moveSpeed调用,所以左蝙蝠将得到它的moveSpeed重置,因此在每个周期执行UpdatePosition调用时,它都会简短地保持在它的重置值。要测试这一点,请尝试在更新位置之前设置moveSpeed?

代码语言:javascript
复制
            leftBat.UpdatePosition(ball, gameTime);
            rightBat.UpdatePosition(ball, gameTime);

            if (gScaleActivated == true)
            {
                leftBat.moveSpeed = 30.0f;
                rightBat.moveSpeed = 30.0f;
            }
票数 0
EN

Stack Overflow用户

发布于 2012-07-03 10:53:16

这太奇怪了。我解决了。我只是简单地注释掉了我的turbo代码。我不知道为什么它与slowMo有任何关系,因为只有当我点击空格键时才会激活它。

代码语言:javascript
复制
                    #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 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11248721

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档