Scene Management in XNA

I have seen many Scene Management codes. Nearly all of them uses a language only advanced developers can understand. Among the codes you feel your lost.

In this tutorial I will make it as simple as it can be. To teach you how very easily "Scene Management" can be done.
All right then lets start with a new project:

scene1.gif

Add these references for the project:

using System.Windows.Forms;
using System.Drawing;

We will be adding windows controls inside xna again

XButton btn     = new XButton();
XButton btn2    = new XButton();     
XButton button1 = new XButton();
XButton button2 = new XButton();

Add these buttons so we will be creating our Intro Screen as "New Game" & "Exit" button shown on.

Add a boolean variable inside Game1.cs

bool onlyonce = true;

then add :

bool uiloaded;

we're controlling our ui if loaded or not

Add another bool variable:

bool newgame_click;

it will control whether our newgame button clicked.

There are 3 screens we will be using in our sample:

* Main Screen(New Game&Exit Buttons)
* Game Intro Screen(Start&Back Buttons)
* Game Screen(No Buttons-"UI LOADED!" Title on Window)

Inside Initialize function, insert your Button objects:

this.IsMouseVisible = true;
          
Control.FromHandle(Window.Handle).Controls.Add(btn);
btn.Location = new System.Drawing.Point(676, 279);
btn.Size = new System.Drawing.Size(100, 40);
btn.Text = "New Game";
btn.ForeColor = System.Drawing.Color.White;
btn.Click += new EventHandler(btn_Click);
btn.Visible = false;

Control.FromHandle(Window.Handle).Controls.Add(btn2);
btn2.Location = new System.Drawing.Point(676, 319);
btn2.Size = new System.Drawing.Size(100, 40);
btn2.Text = "Exit";
btn2.Click+=new EventHandler(btn2_Click);
btn2.ForeColor = System.Drawing.Color.White;
btn2.Visible = false;

//
// button1
//
button1.Location = new System.Drawing.Point(13, 352);
button1.Name = "button1";
button1.Size = new System.Drawing.Size(75, 35);
button1.TabIndex = 28;
Control.FromHandle(Window.Handle).Controls.Add(button1);
button1.Text = "START";
button1.Click+=new EventHandler(Start_Click);
button1.Visible = false;

//
// button2
//
button2.Location = new System.Drawing.Point(100, 352);
button2.Name = "button1";
button2.Size = new System.Drawing.Size(75, 35);
button2.TabIndex = 28;
Control.FromHandle(Window.Handle).Controls.Add(button2);|
button2.Text = "BACK";
button2.Visible = false;
button2.Click+=new EventHandler(Back_Click);

We have created our Button objects.

Create 4 EventHandlers: "btn_Click" & "btn2_Click","Start_Click" & "Back_Click"

protected void Back_Click(Object sender, EventArgs e)
 {
  newgame_click = false;
 }

protected void btn2_Click(Object sender, EventArgs e)
 {
  this.Exit();
 }

protected void btn_Click(Object sender, EventArgs e)
 {
  newgame_click = true;                   
 }

protected void Start_Click(Object sender, EventArgs e)
 {         
  uiloaded = true;          
 }

If we click the button with caption "New Game" then it will set newgame_click's value true. If we click on the Exit Button,it will exit

If we Start it will load the game,which is an empty screen.But you can press "Escape" key to get back.

Inside Draw Function we will be controlling our newgame_click value if true we're loading the new game screen values.

if (newgame_click == false)
{               
   if (onlyonce)
    {                      
      onlyonce = false;
    }                 

    btn.Visible = true;                  
    btn2.Visible = true;                   
    button1.Visible = false;
    button2.Visible = false;            
}

Now lets write the UILoaded codes.

 else
 {
   if (uiloaded)
   {
     button1.Visible = false;
     button2.Visible = false;
     Window.Title = "UI LOADED!";
     KeyboardState newstat = Keyboard.GetState();
     if (newstat.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Escape))
     {
      uiloaded = false;
     }
   }
   else
   {
    btn.Visible = false;
    btn2.Visible = false;
    button1.Visible = true;
    button2.Visible = true;
    onlyonce = true;                   
   }              
 }


LETS RUN THE PROJECT AND SEE WHAT HAPPENS!

scene2.gif

As you can see this is the Main Screen of our project.

If we click on the "New Game" it will open Game Intro Screen which we will see "Start" & "Back" Buttons.

scene3.gif

If we click on the Start it will open an empty game screen which we will be adding our game objects like our characters,UIs,HUDs...

If we click on the Back button it will re-show you the Main Screen.

Lets Click on the Start!

scene4.gif

As I told you an empty screen with "UI LOADED!" caption written on Window's Title property.

That's it! You have successfully created your own simple "Scene Management" Structure.

Wasn't it much more simple?

All the Code:

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Windows.Forms;
using System.Drawing;

namespace SceneMan
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;     

        bool onlyonce = true;
        bool uiloaded;

        XButton btn = new XButton();
        XButton btn2 = new XButton();     
        XButton button1=new XButton();
        XButton button2 = new XButton();

        bool newgame_click;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            Window.Title = "Scene Management";
        }

        protected override void Initialize()
        {
            base.Initialize();
            this.IsMouseVisible = true;

            Control.FromHandle(Window.Handle).Controls.Add(btn);
            btn.Location = new System.Drawing.Point(676, 279);
            btn.Size = new System.Drawing.Size(100, 40);
            btn.Text = "New Game";
            btn.ForeColor = System.Drawing.Color.White;
            btn.Click += new EventHandler(btn_Click);
            btn.Visible = false;

            Control.FromHandle(Window.Handle).Controls.Add(btn2);
            btn2.Location = new System.Drawing.Point(676, 319);
            btn2.Size = new System.Drawing.Size(100, 40);
            btn2.Text = "Exit";
            btn2.Click+=new EventHandler(btn2_Click);
            btn2.ForeColor = System.Drawing.Color.White;
            btn2.Visible = false;

            //
            // button1
            //
            button1.Location = new System.Drawing.Point(13, 352);
            button1.Name = "button1";
            button1.Size = new System.Drawing.Size(75, 35);
            button1.TabIndex = 28;
            Control.FromHandle(Window.Handle).Controls.Add(button1);
            button1.Text = "START";
            button1.Click+=new EventHandler(Start_Click);
            button1.Visible = false;

            //
            // button2
            //
            button2.Location = new System.Drawing.Point(100, 352);
            button2.Name = "button1";
            button2.Size = new System.Drawing.Size(75, 35);
            button2.TabIndex = 28;
            Control.FromHandle(Window.Handle).Controls.Add(button2);
            button2.Text = "BACK";
            button2.Visible = false;
            button2.Click+=new EventHandler(Back_Click);
        }

        protected void Back_Click(Object sender, EventArgs e)
        {
            newgame_click = false;
        }

        protected void btn2_Click(Object sender, EventArgs e)
        {
            this.Exit();
        }

        protected void btn_Click(Object sender, EventArgs e)
        {
            newgame_click = true;                   
        }

        protected void Start_Click(Object sender, EventArgs e)
        {         
            uiloaded = true;          
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);          
        }

        protected override void UnloadContent()
        {

        }

        protected override void Update(GameTime gameTime)
        {
            base.Update(gameTime);           
        }        

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Microsoft.Xna.Framework.Graphics.Color.White);
            if (newgame_click == false)
            {               
                    if (onlyonce)
                    {                      
                        onlyonce = false;
                    }                 

                    btn.Visible = true;                  
                    btn2.Visible = true;                   
                    button1.Visible = false;
                    button2.Visible = false;            
            }
            else
            {
                if (uiloaded)
                {
                    button1.Visible = false;
                    button2.Visible = false;
                    Window.Title = "UI LOADED!";
                    KeyboardState newstat = Keyboard.GetState();
                    if (newstat.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Escape))
                    {
                        uiloaded = false;
                    }
                }
                else
                {
                    btn.Visible = false;
                    btn2.Visible = false;
                    button1.Visible = true;
                    button2.Visible = true;
                    onlyonce = true;                   
                }              
            }           
            base.Draw(gameTime);
        }
    }
}


Similar Articles