c# Xna keydown with a delay of 1 sec
Posted
by bld
on Stack Overflow
See other posts from Stack Overflow
or by bld
Published on 2009-12-04T00:37:05Z
Indexed on
2010/04/01
21:03 UTC
Read the original article
Hit count: 399
c#
Hi!. 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;
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;
}
}
© Stack Overflow or respective owner