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)
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?
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
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.
Hi. I am just wondering how can i load content on those screens? Can I or I must write code in game1 file?
You can make a static class that holds the ContentManager thus having access to it on any screen
Hey, thanks for the tutorial!
Any chance you could update it for XNA 4.0 / VC# 2010?
thanks,
w
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.
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?
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 🙂 .
Whats the different between using this or a singleton pattern ? What benefits
You can use a singleton pattern, but each screen will have different stuff (menus, game itself, credits) so it kind of amounts to the same thing, I guess it really depends on what you are trying to achieve