24
Jan
Xna Screen Manager
I know there’s lot’s of this stuff over the internet but I keep bumping into people asking for this.
A way to easily switch from a Game Screen to a Menu or Options without having tons of flags and “if” clauses on the class Game.
I’ve made a small project with a Screen Manager. The ScreenManager is static and can contain Screens. Instead of having typical Draw Update functions drawing SpriteBatches on the Game class we should have something like this:
protected override void Update(GameTime gameTime) { // Tell ScreenManager to Update SCREEN_MANAGER.Update(gameTime); base.Update(gameTime); } protected override void Draw(GameTime gameTime) { // Tell ScreenManager to draw SCREEN_MANAGER.Draw(gameTime); base.Draw(gameTime); }
This way we are telling the Screen Manager to handle the draws and Updates. And the ScreenManager then tell the active screen to Draw/Update.
In this sample if you press the keys ‘m’ or ‘n’ you can switch from screen1 to screen2 and vice-versa. Notice that the color changes, you are inside a different Screen now.
In order to add another screen it would be something simple as:
class Screen3 : Screen { public Screen1(GraphicsDevice device) :base(device,"screen3") { } public override bool Init() { return base.Init(); } public override void Shutdown() { base.Shutdown(); } public override void Draw(GameTime gameTime) { base.Draw(gameTime); } public override void Update(GameTime gameTime) { base.Update(gameTime); } }
You can tell the Screen Manager to switch screen by doing:
SCREEN_MANAGER.goto_screen("screen3");
Screen Manager then calls the virtual function shutdown() of the current screen and then the init() of the requested Screen, so you might want to always override them for your purposes.
You’ll understand better by viewing the code itself. Use it in any way you want. I now this example may not be the best solution but it gives a good start to those wanting to implement their own.
EDIT:
(04-02-2010) Removed unused member _current; (Thanks Jason)

January 25th, 2010 at 20:35
[...] Dadid Amador adds his own entry to the list of samples that show you how to do screen management in your XNA games. You can read all about it here. [...]
February 4th, 2010 at 13:25
Hi david, excellent Screen Manager, very simple which i appreciate as i’m just cutting my teeth on the concepts. I have a question reguarding a member in the manager ‘static private Screen _current = null;’ this never seems to be used, maybe it was your initial ActiveScreen and just became left out?
February 4th, 2010 at 13:39
Jee you are right. It was my initial member but I changed it and forgot to remove that one. Thanks for pointing that out. Cheers
September 16th, 2010 at 8:22
[...] Xna Screen Manager – clean, simple and easy to use [...]
November 8th, 2010 at 4:50
Hey I know it’s a pretty late reply but I just wanted to say thanks for the post, and especially for the source. XNA is usually pretty well documented but I couldn’t seem to find much about this in the documentation.
Cheers.
November 14th, 2010 at 15:07
Hi. I am just wondering how can i load content on those screens? Can I or I must write code in game1 file?
November 14th, 2010 at 15:22
You can make a static class that holds the ContentManager thus having access to it on any screen
December 9th, 2010 at 8:37
[...] Xna Screen Manager – clean, simple and easy to use [...]
December 12th, 2010 at 22:07
Hey, thanks for the tutorial!
Any chance you could update it for XNA 4.0 / VC# 2010?
thanks,
w
April 29th, 2011 at 1:47
is there any way that you could make a simple example static ContentManager class? I have been trying for hours to get this right! but great job on this, too.
May 15th, 2011 at 2:14
i’m trying to use a particle system within the gameplay screen to no avail. my previous implementation of it was passing it through the graphics (graphics = new GraphicsDeviceManager(this);) in game.cs but i’ve since moved the implementation to the gameplayscreen. is there a way i could still pass it through the graphicsdevicemanager?
May 18th, 2011 at 17:32
Is there a way to load a picture on the menu system, ive tried making a static class with content manager on it, but it keeps giving an error saying the file im trying to load is not found. please help me this is the perfect menu system ive been searching for but i cant load anything on it, its like getting a lamborghini for a birthday present and not being able to open it
.
June 9th, 2011 at 16:17
[...] Xna Screen Manager [...]
August 10th, 2011 at 8:23
[...] Xna Screen Manager – clean, simple and easy to use [...]