我试图在我的第一个xna 2d游戏中模拟重力。我有以下Xna将重力加到2d精灵上
//Used for Jumping
double elapsedAirTime = 0.0;
double maxAirTime = 8.35;
//End Jumping
所以,我想通过一定量的移动精灵了,而elapsedAirTime < maxAirTime 然而,有一些问题,即我的代码似乎只有一次,不能多移动精灵了在这段时间内。这里是我的“player.cs”类中的代码,或者类的更新方法。
if (newState.IsKeyDown(Keys.Space))
{
if(oldState.IsKeyUp(Keys.Space))
{
//if we are standing or jumping then change the velocity
if (playerState == PlayerStates.Standing)
{
playerState = PlayerStates.Jumping;
this.position.Y -= (float)(30.0 + ((1.2)*elapsedAirTime)*elapsedAirTime);
}
}
}
//if we are jumping give it some time
if (playerState == PlayerStates.Jumping)
{
if ((elapsedAirTime < maxAirTime) && position.Y < 3)
{
this.position.Y -= (float)(30.0 + ((1.2) * elapsedAirTime)*elapsedAirTime);
elapsedAirTime += gameTime.ElapsedGameTime.TotalSeconds;
}
//otherwise time to fall
else
{
playerState = PlayerStates.Falling;
}
}
//add gravity to falling objects
if (playerState == PlayerStates.Falling || playerState == PlayerStates.Standing)
{
//if we are above the ground
if (this.position.Y < windowBot - 110)
{
//chnage state to falling
playerState = PlayerStates.Falling;
this.position.Y += 3.0f + ((float)(gameTime.ElapsedGameTime.TotalSeconds));
}
else
{
playerState = PlayerStates.Standing;
elapsedAirTime = 0.0f;
}
}
任何帮助,非常感谢,请和谢谢!
为什么要计算timeScalar并存储旧的游戏时间?如果这个更新函数在每个游戏循环中都被调用(它应该是这样),那么gt.ElapsedGameTime已经为你做了所有繁重的工作。 – 2011-02-09 19:49:09