c#Xna keydown,延迟1秒

时间:2009-12-04 00:37:05

标签: c#

我正在xna写一个俄罗斯方块。我有一个方法rotateBlocks。当我按下“向上”箭头键。我希望当我按住按钮1秒钟或更长时间时它会执行第一个参数中的参数如果(快速旋转块)现在没有任何事情发生。我在类中全局声明了oldState。如果我首先删除游戏时间检查其他如果块将快速旋转imedietley。如果我尝试使用换行符来逐步执行代码,则分辨率可以达到

public void RotateBlocks(loadBlock lb,KeyboardState newState,GameTime gameTime)         {             _elapsedSeconds2 + =(float)gameTime.ElapsedGameTime.TotalSeconds;

        if (lb._name.Equals("block1"))
        {

            if (newState.IsKeyDown(Keys.Up) && !oldState.IsKeyDown(Keys.Up))
            {
                // the player just pressed Up


                if (_rotated)
                {
                    lb._position[0].X -= 16;
                    lb._position[0].Y -= 16;
                    lb._position[2].X += 16;
                    lb._position[2].Y += 16;
                    lb._position[3].X += 32;
                    lb._position[3].Y += 32;
                    _rotated = false;
                }

                else if (!_rotated)
                {
                    lb._position[0].X += 16;
                    lb._position[0].Y += 16;
                    lb._position[2].X -= 16;
                    lb._position[2].Y -= 16;
                    lb._position[3].X -= 32;
                    lb._position[3].Y -= 32;
                    _rotated = true;
                }


            }
            if (newState.IsKeyDown(Keys.Up) && oldState.IsKeyDown(Keys.Up))
            {
                // the player is holding the key down
                if (gameTime.ElapsedGameTime.TotalSeconds >=1)
                {
                    if (_rotated)
                    {
                        lb._position[0].X -= 16;
                        lb._position[0].Y -= 16;
                        lb._position[2].X += 16;
                        lb._position[2].Y += 16;
                        lb._position[3].X += 32;
                        lb._position[3].Y += 32;
                        _rotated = false;
                    }

                    else if (!_rotated)
                    {
                        lb._position[0].X += 16;
                        lb._position[0].Y += 16;
                        lb._position[2].X -= 16;
                        lb._position[2].Y -= 16;
                        lb._position[3].X -= 32;
                        lb._position[3].Y -= 32;
                        _rotated = true;

                    }
                    _elapsedSeconds2 = 0;
                }

            }

3 个答案:

答案 0 :(得分:1)

问题是你的gameTime检查只检查自上次滴答以来已经过了多长时间。

您需要存储第一次按键的时间,然后比较它与当前时间之间的差异。

答案 1 :(得分:0)

ElapsedGameTime是自上次调用Game.Update()以来的时间。因此,如果游戏循环以每秒60次的速度运行,则ElapsedGameTime将始终为1/60秒。您需要在最初按下按键时记录TotalGameTime,并在将来的更新中用于比较。

答案 2 :(得分:0)

@Michael是正确的,你需要做类似

的事情
// where integertype is int or long etc
integertype startPress = 0;

public void RotateBlocks(loadBlock lb, KeyboardState newState, GameTime gameTime) {

    _elapsedSeconds2 += (float)gameTime.ElapsedGameTime.TotalSeconds;

  if (lb._name.Equals("block1"))
        {
            if (newState.IsKeyDown(Keys.Up) && !oldState.IsKeyDown(Keys.Up))
            {
                // the player just pressed Up
startPress = GameTime.ElapsedGameTime.TotalSeconds;

                if (_rotated)
                {
                    lb._position[0].X -= 16;
                    lb._position[0].Y -= 16;
                    lb._position[2].X += 16;
                    lb._position[2].Y += 16;
                    lb._position[3].X += 32;
                    lb._position[3].Y += 32;
                    _rotated = false;
                }

                else if (!_rotated)
                {
                    lb._position[0].X += 16;
                    lb._position[0].Y += 16;
                    lb._position[2].X -= 16;
                    lb._position[2].Y -= 16;
                    lb._position[3].X -= 32;
                    lb._position[3].Y -= 32;
                    _rotated = true;
                }


            }
            if (newState.IsKeyDown(Keys.Up) && oldState.IsKeyDown(Keys.Up))
            {
                // the player is holding the key down
                if (gameTime.ElapsedGameTime.TotalSeconds - startPress >=1)
                {
                    if (_rotated)
                    {
                        lb._position[0].X -= 16;
                        lb._position[0].Y -= 16;
                        lb._position[2].X += 16;
                        lb._position[2].Y += 16;
                        lb._position[3].X += 32;
                        lb._position[3].Y += 32;
                        _rotated = false;
                    }

                    else if (!_rotated)
                    {
                        lb._position[0].X += 16;
                        lb._position[0].Y += 16;
                        lb._position[2].X -= 16;
                        lb._position[2].Y -= 16;
                        lb._position[3].X -= 32;
                        lb._position[3].Y -= 32;
                        _rotated = true;

                    }
                    _elapsedSeconds2 = 0;
                }

            }