7 Replies - 6824 Views - Last Post: 06 May 2012 - 04:13 PM

#1 2lost4u   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 03-May 12

XNA Game State Management Question

Posted 03 May 2012 - 01:08 PM

Hi all of you.-

Im from Mexico so be patient with my English, please. Well my question is the following, I have searched around the net a lot about gamestates in XNA and I found App Hub - Game State Management example for download, ok so I download it and at first i coudnt understand it very well but now i think i got it, my question is the example demostrates a game state management, but it only implements two screens background and menuscreen, i want more states in my game like a Splash screen, a Thanks screen, a menu screen, a gameover screen and so on, now how can i change between states based on that example, so far i made a timer and a enum to change between states, the only problem is that when i draw in a state, it doesnt do it correctly, my video doesnt show the sound of the video plays even in other game states, whats the correct aproach in this? sorry if this is too long.

Is This A Good Question/Topic? 0
  • +

Replies To: XNA Game State Management Question

#2 2lost4u   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 03-May 12

Re: XNA Game State Management Question

Posted 03 May 2012 - 01:38 PM

I have forgotten to tell you, if you need to see my code only ask
Was This Post Helpful? 0
  • +
  • -

#3 anonymous26   User is offline

  • D.I.C Lover

Reputation: 2
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: XNA Game State Management Question

Posted 03 May 2012 - 03:32 PM

People always need to see code.
Was This Post Helpful? 0
  • +
  • -

#4 Gonzo0193   User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 49
  • Joined: 16-December 09

Re: XNA Game State Management Question

Posted 03 May 2012 - 03:43 PM

I did my gamestate by having a game state manager class and then in my main update using a switch such as:

switch(gameState.currentState)
{
case "titleScreen":
    {
        //do title screen stuff here
        break;
    }

case "game":
    {
        //do game stuff here
        break;
    }

default: break;

}




Not sure if thats the correct way to do it but it works for me :)
Was This Post Helpful? 0
  • +
  • -

#5 2lost4u   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 03-May 12

Re: XNA Game State Management Question

Posted 03 May 2012 - 04:04 PM

Thanks everyone Gonzo0193 my code is the following.-

Each class is a child from DrawableGameComponent

SplashScreen.-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 X_Type.ScreenManagerClass;
using X_Type;

namespace X_Type.Screens
{
    public class SplashScreen : DrawableGameComponent
    {
        //KeyboardState OldKeyState;
        Video video;
        public VideoPlayer player;
        Texture2D videoTexture;

        Game1.GameStates CurrentState = Game1.GameStates.Logo;
        //CurrentState = new Game1.GameStates;

        public SplashScreen(Game game) : base(game)
        {
            /*
            this.ScreenManager.AddScreen(new SplashScreen(), Microsoft.Xna.Framework.PlayerIndex.One);
            this.ExitScreen();
             */
            
            game.Components.Add(this);
        }

       
        public override void Initialize()
        {
            
            base.Initialize();
        }

        protected override void LoadContent()
        {
            video = Game.Content.Load<Video>("X-Type");
            
            player = new VideoPlayer();
            base.LoadContent();
        }

        protected override void UnloadContent()
        {
            
            base.UnloadContent();
        }
        /// <summary>
        /// Updates the menu.
        /// </summary>        
        /*public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {            
            
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
            
        }
        */

        public override void Update(GameTime gameTime)
        {
            //this.Visible = false;
            switch (CurrentState)
            {
                case Game1.GameStates.Logo:
                    if (player.State == MediaState.Stopped)
                    {
                        player.IsLooped = false;
                        player.Play(video);
                        break;
                    }

                    break;
            }
            base.Update(gameTime);
        }
                
        /// <summary>
        /// Draws the menu.
        /// </summary>
        public override void Draw(GameTime gameTime)
        {            
            /*GraphicsDevice graphics = this.GraphicsDevice;
            SpriteBatch spriteBatch = this.SpriteBatch;
            SpriteFont font = ScreenManager.Font;
            graphics.Clear(Color.Green);
            */ 
            Game.GraphicsDevice.Clear(Color.DarkGreen);

            SpriteBatch spriteBatch;
            
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);

            // Only call GetTexture if a video is playing or paused
            if (player.State != MediaState.Stopped)
                videoTexture = player.GetTexture();

            // Drawing to the rectangle will stretch the 
            // video to fill the screen
            Rectangle screen = new Rectangle(Game.GraphicsDevice.Viewport.X,
                Game.GraphicsDevice.Viewport.Y,
                Game.GraphicsDevice.Viewport.Width,
                Game.GraphicsDevice.Viewport.Height);

            // Draw the video, if we have a texture to draw.
            if (videoTexture != null)
            {
                spriteBatch.Begin();
                spriteBatch.Draw(videoTexture, screen, Color.White);
                spriteBatch.End();
            }

            base.Draw(gameTime);
        }        
    }
}




Thanks Screen.-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 X_Type.ScreenManagerClass;
using X_Type;

namespace X_Type.Screens
{
    public class ThanksScreen : DrawableGameComponent
    {

        SpriteFont SegoeX;
        //SpriteBatch spriteBatch;
        Vector2 FontPos;
        Vector2 FontPosDos;
        float FontRotation;

        public ThanksScreen(Game game) : base(game)
        {
            game.Components.Add(this);

        }

        
        protected override void LoadContent()
        {
            FontRotation = 0;
            // Create a new SpriteBatch, which can be used to draw textures.
            //spriteBatch = new SpriteBatch(graphics);
            SegoeX = Game.Content.Load<SpriteFont>("Segoe");
            
                     
            base.LoadContent();
        }
        
        

        public override void Initialize()
        {
            base.Initialize();
        }

        protected override void UnloadContent()
        {
            base.UnloadContent();
        }


        /// <summary>
        /// Updates the menu.
        /// </summary>        
        public override void Update(GameTime gameTime)
        {
            //this.Visible = false;
            base.Update(gameTime);
        }

        /*
        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {

            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

        }
        */

        /// <summary>
        /// Draws the menu.
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            /*
            GraphicsDevice graphics = ScreenManager.GraphicsDevice;
            
             */
            SpriteBatch spriteBatch;
            SpriteFont font;

            font = Game.Content.Load<SpriteFont>("Segoe");
            
            Game.GraphicsDevice.Clear(Color.Black);

            spriteBatch = new SpriteBatch(Game.GraphicsDevice);
            
            spriteBatch.Begin();
            string outputThanks = ("Agradecimientos a mi Familia, que siempre me han apoyado, a mis amigos,");
            string outputThanksDos = ("que aunque son pocos son buenos y a todos los programadores.");
            // Find the center of the string
            Vector2 FontOrigin = SegoeX.MeasureString(outputThanks) / 2;
            Vector2 FontOriginDos = SegoeX.MeasureString(outputThanksDos) / 2;
            FontPos = new Vector2(Game.GraphicsDevice.Viewport.Width / 2, 80);
            FontPosDos = new Vector2(Game.GraphicsDevice.Viewport.Width / 2, FontPos.Y + 30);
            // Draw the string
            spriteBatch.DrawString(SegoeX, outputThanks, FontPos, Color.White, FontRotation, FontOrigin, 1.0f, SpriteEffects.None, 0.5f);
            spriteBatch.DrawString(SegoeX, outputThanksDos, FontPosDos, Color.White, FontRotation, FontOriginDos, 1.0f, SpriteEffects.None, 0.5f);
            spriteBatch.End();
        }
    }
}




Game1

using System;
using System.Collections.Generic;
using System.Linq;
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 X_Type.Screens;
using X_Type.ScreenManagerClass;
using System.Timers;

namespace X_Type
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        public GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        SplashScreen Splash;
        ThanksScreen Thanks;
        MenuScreen Menu;
        

        public LevelLoader mLevelConfusion;
        /*ScreenManager screenManager;
        SplashScreen sc;
        ThanksScreen ts;
        BackgroundScreen bs;
        MainMenuScreen mms;
        */

        public enum GameStates
        {
            Logo,
            Init,
            Thanks,
            Menu,
            Confusion,
            Victory,
            GameOver
        }

        GameStates CurrentState = GameStates.Logo;
        KeyboardState OldKeyState;
        


        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.IsFullScreen = true;
            /*
            screenManager = new ScreenManager(this);
            Components.Add(screenManager);
            */

            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            mLevelConfusion = new LevelLoader(@"./XML/Data.xml", this.Content);
            
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
                        
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            KeyboardState NewKeyState = Keyboard.GetState();
            
                        
            switch (CurrentState)
            {

                case GameStates.Logo:

                    TimeSpan lTime;
                    //cTime = new TimeSpan(0, 0, 0);
                    
                    lTime = gameTime.TotalGameTime;
                    if (NewKeyState.IsKeyDown(Keys.Escape) && OldKeyState.IsKeyUp(Keys.Escape))
                    {
                        //sc.ExitScreen();
                        OldKeyState = NewKeyState;
                        CurrentState = GameStates.Init;
                        break;

                    }

                    /*if (lTime.Seconds >= 23)
                    {
                        OldKeyState = NewKeyState;
                        CurrentState = GameStates.Init;
                        break;

                    }
                    */

                    if (Splash.player.State == MediaState.Stopped)
                    {
                        CurrentState = GameStates.Init;
                        OldKeyState = NewKeyState;
                        break;
                    }

                    OldKeyState = NewKeyState;
                    break;

                case GameStates.Init:
                    TimeSpan iTime;
                    iTime = gameTime.TotalGameTime;
                    if (NewKeyState.IsKeyDown(Keys.Escape) && OldKeyState.IsKeyUp(Keys.Escape))
                    {
                        CurrentState = GameStates.Thanks;
                    }

                    if (iTime.Seconds >= 10)
                    {
                        CurrentState = GameStates.Thanks;
                        break;

                    }

                    OldKeyState = NewKeyState;
                        
                    break;

                case GameStates.Thanks:
                    TimeSpan tTime;
                    tTime = gameTime.TotalGameTime;
                    if (NewKeyState.IsKeyDown(Keys.Escape) && OldKeyState.IsKeyUp(Keys.Escape))
                    {
                        //ts.ExitScreen();
                        CurrentState = GameStates.Menu;
                    }

                    if (tTime.Seconds >= 25)
                    {
                        CurrentState = GameStates.Menu;
                        break;

                    }

                    OldKeyState = NewKeyState;

                    break;

                case GameStates.Menu:
                    if (NewKeyState.IsKeyDown(Keys.Escape) && OldKeyState.IsKeyUp(Keys.Escape))
                    {
                        CurrentState = GameStates.Confusion;
                    }
                    
                    OldKeyState = NewKeyState;
                    
                    break;

                case GameStates.Confusion:
                    if (NewKeyState.IsKeyDown(Keys.Escape) && OldKeyState.IsKeyUp(Keys.Escape))
                    {
                        this.Exit();
                    }

                    OldKeyState = NewKeyState;
                    //CurrentState = GameStates.Victory;
                    break;

                case GameStates.Victory:

                    break;

                case GameStates.GameOver:

                    break;

            }

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            //GraphicsDevice.Clear(Color.CornflowerBlue);

            switch (CurrentState)
            {
                case GameStates.Logo:
                    //GraphicsDevice.Clear(Color.DarkGreen);
                    /*
                    sc = new SplashScreen();
                    screenManager.AddScreen(sc, null);
                    */
                    Splash = new SplashScreen(this);
                    break;

                case GameStates.Init:                    
                    GraphicsDevice.Clear(Color.Tomato); 
                    break;

                case GameStates.Thanks:
                    Thanks = new ThanksScreen(this);
                    //GraphicsDevice.Clear(Color.Black);
                    /*ts = new ThanksScreen();
                    screenManager.AddScreen(ts, null);*/
                    break;

                case GameStates.Menu:
                    /*bs = new BackgroundScreen();
                    mms = new MainMenuScreen();
                    screenManager.AddScreen(bs, null);
                    screenManager.AddScreen(mms, null);*/
                    Menu = new MenuScreen(this, "Menu");
                    GraphicsDevice.Clear(Color.Aqua);
                    break;

                case GameStates.Confusion:
                    GraphicsDevice.Clear(Color.Yellow);

                    spriteBatch.Begin();
                    
                    mLevelConfusion.Draw(this.spriteBatch);
                    
                    spriteBatch.End();
                    
                    break;

                case GameStates.Victory:

                    break;

                case GameStates.GameOver:

                    break;

            }

            base.Draw(gameTime);
        }
    }
}




The other game state clases are almost empty but they are a part of DrawableGameComponent, as you can see ive tried to implement a timer to switch automaticaly between game states it works ok, but when i draw someting everyting fails
Was This Post Helpful? 0
  • +
  • -

#6 Kilorn   User is offline

  • XNArchitect
  • member icon



Reputation: 1361
  • View blog
  • Posts: 3,534
  • Joined: 03-May 10

Re: XNA Game State Management Question

Posted 03 May 2012 - 06:39 PM

I always use a slightly modified version of the Game State Management tutorial that was written by our own SixOfEleven. Here is a link to his tutorial section. The Game State Management tutorials are at the bottom of that list and have been written for XNA 3.0/3.1, but they still work for XNA 4.0, with maybe a slight change here and there. I'd suggest looking through his as his is about the best I've seen, and he has a way of making advanced concepts easier to grasp. Definitely worth looking through, even if you decide not to use it in the end.
Was This Post Helpful? 0
  • +
  • -

#7 2lost4u   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 03-May 12

Re: XNA Game State Management Question

Posted 04 May 2012 - 12:11 PM

Thank you XNArchitect.-

Ill see the tutorials with care and i will post my findings here. Thanks
Was This Post Helpful? 0
  • +
  • -

#8 2lost4u   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 03-May 12

Re: XNA Game State Management Question

Posted 06 May 2012 - 04:13 PM

Ok so the key of the implementation in Kilornīs suggested link is that every DrawableGameComponent has a Hide and Enabled method, that way you can hide every GameScreen, so what im going to do is add a switch in the activeScreen variable so i can change between gamestates easily hope that works, and thank you Kilorn and everybody real great tutorials.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1