XNA Game State Management Start kit

Started by
3 comments, last by Machaira 13 years, 11 months ago
Hey I'm using the Game State Management kit that you can download from the Creators Club. At the moment I'm using it for a 2D platformer game I'm working on with some friends. We have it set up so that each level (a text file) is loaded via stream reader (it reads the symbols in the text file and draws the corresponding sprite) and that progression from one level to the next is done through an Index we called levelIndex. Now, once the last level has been completed, the game simply crashes as it can't do anything. Could someone please help me by showing me how I can get the game to transition back to the main menu screen after the last level has been completed? I've spent a good few hours looking through each of the classes that come's with the starter kit but I can't find the part that changes you from one screen to the next and back again.
Advertisement
I'm still fairly new to this, so if someone else has a better idea, ignore me :)

what I would do is have an enum with the different states of the game (main menu, level 1, level 2, ... , game over)

then, i would create a variable of the enum (there has to be a name for it, but idk) and set it's value to whatever state your game will be in when the game starts.

from there, I would put a switch statement in the update and draw methods to control what the game should do in the given state.

For instance:
[Source]enum gameState{    mainMenu = 1,    levelOne = 2,    levelTwo = 3, // however many levels you have    gameOver = 4};gameState currentGameState = gameState.mainMenu; // whatever the game should be in whenever the game is first ran//inside your update methodpublic void Update(){    switch (currentGameState)    {        case gameState.mainMenu:            //check the users input to switch between states            //if (down Key is pressed)                //currentGameState--;                            //check to make sure the user hasnt tried to go past the lowest option            //if (currentGameState <= gamestate.mainMenu)                //currentGameState = gamestate.mainMenu            //Repeat for the up key here            break;            //do the same for the other cases, adding game update logic wherever necessary    }}public void Draw(){    switch (currentGameState)    {        case gameState.mainMenu:            //do any draw calls needed for the mainMenu            //you should probably have text that changes color depending on the current menu state (for options, 1p, 2p, etc)            //that can be done with another enum and a switch inside of the mainMenu case            break;        //do the same for the other cases    }}[/Source]
What I would do is have a stack of game states. Whenever you enter a new game state, you push it onto this stack. Whenever you exit a state, you pop the state off the stack. Once this happens, because the previous game state is now at the top of the stack, it is now the active state and you can process it accordingly. For example, let's say that you have a menu game state where you can select various game play options, start new games, quit, etc. When you decide to start a new game, the game state system would suspend the menu state and push the actual gameplay state on top of the state stack. Your game would then run the gameplay state as usual. When the gameplay state exits, it pops itself off the stack and the menu game state (which was immediately beneath it on the stack) is restored.
Thanks for the replies. Though these aren't the solutions to what I was asking for.

What I said in the first line was that I'm using the game state management code you can get from www.creators.xna.com. The link is here: http://creators.xna.com/en-US/samples/gamestatemanagement


Whilst Enums are great for game states (I used them for my own XNA and DirectX stuff) the project I am using makes use of the Creators Club game state. To change it to a enum or any other type of game state is not an option. We are too far ahead in our game. Don't think I don't appreciate your answers.

If someone could please help me make my games transition to the start screen after the last level has been completed. I would be appreciative.
Wherever it is that your code checks for the next level all you should have to do is:

this.ScreenManager.AddScreen(new MainMenuScreen(), null);
this.ExitScreen();

Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development

This topic is closed to new replies.

Advertisement