Reputation: 51
I am working on a game in XNA and when I start the game a menu pops up. If u press "space" the game itself will start. I am using a gamestate switch with three different cases to do this. Although when I get to the last state (the gameover screen) and want to restart the game by pressing "space" it's not working unless you HOLD DOWN "space".
So basically. The game starts with a menu screen. If you press space the game itself will start. If you collide with anything and die, you get to the gameover screen. I want it so that you can go back to the gamerunning if you press space, this is not working (unless you hold down space and keep it that way)
PS: I am willing to add more code if it's needed, just ask for it. Thanks in advance!
Here's my update code:
const int introMenu = 0, gameRunning = 1, gameOver = 2;
switch (gameState)
{
case introMenu:
if (ks.IsKeyDown(Keys.Space))
{
gameState = gameRunning;
}
break;
case gameRunning:
if(colliding)
{
gameState = gameOver;
}
break;
case gameOver:
if (ks.IsKeyDown(Keys.Space))
{
gameState = gameRunning; //NOT WORKING!
}
break;
}
Here's my draw code:
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
switch (gameState)
{
case introMenu:
background.Draw(spriteBatch);
break;
case gameRunning:
//draw the player, new background and everything nessecary
break;
case gameOver:
//draw new gameover background
break;
}
spriteBatch.End();
base.Draw(gameTime);
Upvotes: 2
Views: 3458
Reputation: 21187
The answer above and comment are completely correct. To fix this individual problem just change your code as follows:
if(colliding)
{
colliding = false;
gameState = gameOver;
}
The problem is happening because when you press the space bar is pressed after a game over the gamestate switches to running and then instantly back to the game over screen because colliding is still true
Upvotes: 2
Reputation: 31204
Now I don't have enough to come up with an exact answer, but if I were to guess, your colliding
condition remains true, and the gamestate just goes back.
As I said in my comment, you can use the debugger to check whether or not this is actually the case.
I would also expect you to reinitialize the game variables back to it's original state, so when you start the game over again, it's as though you started it for the first time(and make sure colliding is not true)
Upvotes: 4