0
\$\begingroup\$

I am trying to make a point-and-click adventure game with XNA, starting off simple. My experience with XNA is about a month old now, know how the classes and inheritance works (basic stuff).

I have a problem where I cannot understand how I should load and unload the textures and game objects in the game, when the player transitions to another level. I've googled this >10 times, but all I find is hard coding while I don't even understand the basics of unloading yet.

All I want, is transitioning to another level (replacing all the sprites with new ones).

Thanks in advance

\$\endgroup\$
  • \$\begingroup\$ What have you tried? Is there something that's specifically not working about Content.Load that you expected? \$\endgroup\$ – craftworkgames Jun 1 '14 at 23:17
  • \$\begingroup\$ I have found this post, which I am using to try out stuff now. I don't understand half of it, I noticed. I think need to figure out how contentmanagers work and how to write my own. \$\endgroup\$ – Daabri Jun 1 '14 at 23:42
0
\$\begingroup\$

The way I handle level transitions is through game states. Let me give a little example:

Game states of: TitleScreen, Menu, Level1, Level2, Level3, Credits, Pause

So each game state can use it's own LoadContent and UnloadContent.

When TitleScreen is active, LoadContent loads a Title Image, then unloads them. When the Menu becomes active, it loads any graphics need with the menu, then unloads them. When Level1 becomes active, it loads all it's assets etc.

The only difference is a Pause game state, in which it doesn't load or unload, it just sits and doesn't call GameUpdate until the "unpause" is pressed.

How you manage gamestates is up to you. I like to create an enum named GameState

Something like this

public enum GameState
{
    TitleScreen,
    Menu,
    Level1,
    Level2,
    Level3,
    Pause
};

GameState currentGameState = GameState.TitleScreen;

Then just use a switch

switch(this.gameState)
    {
        case GameState.TitleScreen:   
            //Load TitleScreen Stuff
            break;
       case GameState.Menu:
            //Load Menu Stuff
            break;
       case GameState.Level1:
            //Load Level 1 assets
            break;
        //etc

        default:
            throw new Exception("Something Broke.");
        break;

    }

You can then leverage the gamestates to cleanly handle your loading and unloading of game assets.

\$\endgroup\$
  • \$\begingroup\$ If you are going to use states why not use a state machine? \$\endgroup\$ – ClassicThunder Jun 2 '14 at 15:31
  • \$\begingroup\$ This is very helpful! The most problem I had was recognising a good pattern for this. I made something similar like this before you answered, but this clear a lot up. I also need to dig deeper into basic XNA stuff I noticed, since I do not understand enums yet while they help tremendously. Thanks! \$\endgroup\$ – Daabri Jun 9 '14 at 15:12
  • \$\begingroup\$ This is a reasonable approach if your game is small, but it won't scale very well for large games since you'll need to update the switch statement every time you add a new level. A more flexible approach is to use a strategy pattern en.wikipedia.org/wiki/Strategy_pattern \$\endgroup\$ – craftworkgames Jun 9 '14 at 23:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.