XNA Screen Manager

 

Developing a game i have found is very different from developing a business application.  In a business application you will have a menu, then a series of screens which will as a rule run independently and have similar behaviour, the user will enter data and save this to the system

A Game however will behave differently depending on what screen you are on,the main screen will contain a menu to start the game,view instructions and exit the game.  The user will press Up or Down and select the appropriate option. The Gameplay screen will be completely different, you will have a main Gameplay Loop that iterates throughout the game, updating the position of enemies, drawing on the screen and handling updates.  It will also be possible for this screen to be left immediately for example if they go to a Pause screen.

There is sample XNA code available called ScreenManager which can be used. If you google it you will soon find it

This is how you call it

screenManager = new ScreenManager(this,graphics);

Components.Add(screenManager);

 

You can then add screens using the following screens

screenManager.AddScreen(new MainMenuScreen(), null);

 

You can then use this for all the screens including the Gameplayscreen which will contains the Gameplay screen.,

To enable this function the screen will need to inherit from the Gamescreen class.

Put your own logic into the Update method in the Screen Manager class shown below to control what screen is shown or when you need your Gameplay screen to show a new level

public override void Update(GameTime gameTime)
       {
           // Read the keyboard and gamepad.
           input.Update();

           // Make a copy of the master screen list, to avoid confusion if
           // the process of updating one screen adds or removes others.
           screensToUpdate.Clear();

           foreach (GameScreen screen in screens)
               screensToUpdate.Add(screen);

           bool otherScreenHasFocus = !Game.IsActive;
           bool coveredByOtherScreen = false;

           // Loop as long as there are screens waiting to be updated.
           while (screensToUpdate.Count > 0)
           {
               // Pop the topmost screen off the waiting list.
               GameScreen screen = screensToUpdate[screensToUpdate.Count – 1];

               screensToUpdate.RemoveAt(screensToUpdate.Count – 1);

               if (screen.isLevelChange == true)
               {
                   //new leve
                   PlayerIndex e = (PlayerIndex)screen.ControllingPlayer;

                   if (icurrentLevel == 14)
                   {
                       icurrentLevel = 1;
                   }
                   else{

                       icurrentLevel++;
                   }

                       // I think isLevelchange should be false
                       LoadingScreen.Load(this, true, e, false,
                                 new GamePlayScreen(icurrentLevel));
                       // Just added this
                       //screen.isLevelChange == false;
                       RemoveScreen(screen);
                       // Just added this
                       //AddScreen(new GamePlayScreen(icurrentLevel),e);
               //    }
               }
               else
               {

                   // Update the screen.
                   screen.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

                   if (screen.ScreenState == ScreenState.TransitionOn ||
                       screen.ScreenState == ScreenState.Active)
                   {
                       // If this is the first active screen we came across,
                       // give it a chance to handle input.
                       if (!otherScreenHasFocus)
                       {
                           screen.HandleInput(input);

                           otherScreenHasFocus = true;
                       }

                       // If this is an active non-popup, inform any subsequent
                       // screens that they are covered by it.
                       if (!screen.IsPopup)
                           coveredByOtherScreen = true;
                   }
               }
           }

           // Print debug trace?
           if (traceEnabled)
               TraceScreens();
       }

      

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: