Reputation: 284
I'm trying to have my SplashScreen state come up first which worked before I used my Thread.Sleep(2300); but now it always goes to the menu state first. I need help getting it to go to the SplashScreen state before menu and wait 2300 miliseconds THEN go to the menu state.
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 System.Threading;
namespace Obsession
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
enum _gameState { Splashscreen, Menu, Options, DLC, Playing , Exit };
_gameState currentState;
Vector2 caravanPos = new Vector2(0, 0);
Texture2D caravanSplash;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
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()
{
Window.Title = "Obsession DB 1.0";
graphics.PreferredBackBufferWidth = 800;
graphics.PreferredBackBufferHeight = 600;
//if (graphics.IsFullScreen != true)
//graphics.ToggleFullScreen();
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);
caravanSplash = this.Content.Load<Texture2D>("data/textures/splash1");
currentState = _gameState.Splashscreen;
}
/// <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();
switch (currentState)
{
case _gameState.Splashscreen:
{
SplashScreenUpdate();
break;
}
case _gameState.Menu:
{
MenuUpdate();
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);
spriteBatch.Begin();
switch (currentState)
{
case _gameState.Splashscreen:
{
SplashScreenDraw();
break;
}
case _gameState.Menu:
{
MenuDraw();
break;
}
}
spriteBatch.End();
base.Draw(gameTime);
}
private void SplashScreenUpdate()
{
Thread.Sleep(2300);
currentState = _gameState.Menu;
/*for (int i = 0; i < 5; i++)
{
if (Keyboard.GetState().IsKeyDown(Keys.A) == true)
{
Thread.Sleep(3000);
currentState = _gameState.Menu;
return;
}
}*/
}
private void SplashScreenDraw()
{
spriteBatch.Draw(caravanSplash, caravanPos, Color.White);
}
private void MenuUpdate()
{
if (Keyboard.GetState().IsKeyDown(Keys.A) == true)
{
currentState = _gameState.Splashscreen;
return;
}
}
private void MenuDraw()
{
GraphicsDevice.Clear(Color.White);
}
}
}
Upvotes: 0
Views: 1823
Reputation: 66
Agreed with MrME that its better to set the currentState inside your Initialize method.
The reason it is always going to the menu and never your splash is because of the Thread.Sleep(2300)
.
XNA calls the update method first, followed by the draw method. Your application never gets to the draw method because your update method causes you to sleep, then to switch to the menu state. As Ben stated in his answer, you can use the if(gameTime.TotalGameTime.TotalMilliseconds > 2300)
to perform that conditional, and once that conditional is tripped, the game will update.
I figured it was better to explain why it was broken rather than just how to fix it.
Upvotes: 0
Reputation: 453
Don't do Thread.Sleep();
but rather if(gameTime.TotalGameTime.TotalMilliseconds > 2300)
.
Upvotes: 2