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;
}
}
答案 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;
}
}