Estoy tratando de simular la gravedad en mi primer juego xna 2d. Tengo el siguienteXna agregando gravedad a un sprite 2d
//Used for Jumping
double elapsedAirTime = 0.0;
double maxAirTime = 8.35;
//End Jumping
así que estoy tratando de mover el sprite por una cierta cantidad mientras que el elapsedAirTime < maxAirTime Sin embargo, hay un cierto problema por el que el código sólo parece mover el sprite una vez y que no sea múltiplo veces durante este segmento de tiempo. aquí está el código en mi clase "player.cs", o el método de actualización de la clase.
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;
}
}
¡Cualquier ayuda es muy apreciada, por favor y gracias!
¿Por qué calcular un timeScalar y almacenar el tiempo de juego anterior?Si esta función de actualización se llama cada ciclo de juego (como debería ser), gt.ElapsedGameTime ya hace todo el trabajo pesado por ti. –
La razón para usar timeScalar es a) si desea cambiar la velocidad de fotogramas predeterminada, o b) si su computadora es tan lenta que no puede mantenerse al día con los 60 UPS/FPS predeterminados que intenta XNA. En cualquier caso, si te quedas atrás de la tasa de actualización objetivo, tu juego se verá retrasado sin el código timeScalar. – Darkhydro