-1

So I'm using enum for game states. When I am in the game playing state I can pause and return to the main menu. But if I click Play game and move to game playing again, everything is as I left it. I'm looking for a way to somehow "restart/refresh/flush" the game playing state. So if I go back to it I will have to start over.

2
  • sounds like you need to implement so 'Restart/ Continue/` functionality etc so if you have something you want or need to add in regards to the enum add it.. also show some code so we can see what your enum looks like Commented Oct 17, 2014 at 19:24
  • 1
    Just off the top of my head: what about possibly adding a NewGame state that would switch to Playing after init / setup is complete? Commented Oct 17, 2014 at 19:27

2 Answers 2

1

You have some code now (one presumes) that initializes your game state when the program starts. You need to factor that code out (if necessary) so that it can also be called to initialize the game state any time you want. Such as when you are starting a new game, i.e. moving your game state to "game playing".

Indeed, most of the initialization code (i.e. everything except the stuff that doesn't need to be reinitialized on a new game), you'll probably want to run only then. Running it when you start up the program and then again later is redundant and will just slow boot times down.

Sign up to request clarification or add additional context in comments.

3 Comments

You mean I should put it in Initialize()?
I don't know. I don't do XNA, so I don't know what "Initialize()" is or when it gets called. But, assuming that's a method that XNA calls automatically when you switch to the "game playing" state from the "main menu" state, yes...that sounds like a good place to put your initialization. :)
Peter's answer "need to factor that code out" is generally correct, but don't put it in Initialize . The reason is because XNA's Game.Initialize() is called only once and that is during startup. See above link for more details. You'll need to create a new method and invoke it from Game.Update when appropriate
0

My gameplay class have lists like "Enemies", "Explosions", "Bullets"... and inside Gampleay class i have Init sub that is executed when is state changed to "gameplay". It clears all and load level.

in pseudo code looks like this:

Class GamePlay Sub Init Enemies.Clear() Explosions.Clear() Bullets.Clear() Paralax.Clear() Stars.Clear() LoadLevel(lvl) End Sub Sub LoadLevel(lvl) Stars.Geneate(200) Paralax.Add(new Paralax(type, position, velocity)) End Sub Sub Update if (mainship.fire) bullets.fire() End Sub Sub Draw End Sub End Class 

1 Comment

Whilst a fine answer for initial game startup, it does not address the OP's desire of how to "restart/refresh/flush" via a state machine

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.