开发者

c# Xna keydown with a delay of 1 sec

im writing a tetris in xna. i have a class with a method rotateBlocks. When i press the "Up" arrow key. i wanna have that when i hold the button down for 1 sec or more that it executes the arguments in the first else if(rotating the blocks fast) right now nothing is happening. i have declared oldState globally in the class. if i remove the gametime check in first the else if the block will rotate fast imedietley. if i try to step through the code with linebreaks the resolution get f****d up

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;开发者_Go百科
                        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;
                }

            }


The problem is that your gameTime check is only checking how long has elapsed since the previous tick.

You need to store the time when they first press the key, and then compare the difference between that and the current time.


ElapsedGameTime is the time since the last time Game.Update() was called. So if you game loop is running at a constant 60 times a second, ElapsedGameTime would always be 1/60 second. You need to record TotalGameTime when the key is initially pressed, and use that in comparisons in future updates.


@Michael is correct, you need to do something like

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

            }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜