我正在尝试让我的SplashScreen状态首先出现,然后才使用我的Thread.Sleep(2300);但现在它总是首先进入菜单状态。我需要帮助让它在菜单之前进入SplashScreen状态并等待2300毫秒然后进入菜单状态。
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);
}
}
}
答案 0 :(得分:2)
不要Thread.Sleep();
而是if(gameTime.TotalGameTime.TotalMilliseconds > 2300)
。
答案 1 :(得分:0)
同意MrME,最好在Initialize方法中设置currentState。
它总是进入菜单的原因,而不是因为Thread.Sleep(2300)
而引起轰动。
XNA首先调用update方法,然后调用draw方法。您的应用程序永远不会进入draw方法,因为您的更新方法会让您进入休眠状态,然后切换到菜单状态。正如Ben在他的回答中所说,你可以使用if(gameTime.TotalGameTime.TotalMilliseconds > 2300)
来执行该条件,一旦条件被触发,游戏就会更新。
我认为最好解释为什么它被破坏而不仅仅是如何解决它。