2013-03-25 77 views
0

我试图让我的SplashScreen状态第一次工作之前,我用我的Thread.Sleep(2300);但现在它总是首先进入菜单状态。我需要帮助让它进入菜单前的SplashScreen状态并等待2300毫秒,然后进入菜单状态。XNA 4 C#游戏状态 - 菜单状态第一个

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

你确实运行了一个断点,它完全跳过了闪屏?除此之外,最好在Initialize()中设置currentState – MrME 2013-03-25 19:22:17

回答

2

不要做Thread.Sleep();而是if(gameTime.TotalGameTime.TotalMilliseconds > 2300)

0

与MrME同意,最好在Initialize方法中设置currentState。

它总是会进入菜单并且从来没有溅过的原因是因为Thread.Sleep(2300)

XNA先调用update方法,然后调用draw方法。您的应用程序永远不会进入绘制方法,因为您的更新方法会导致您进入睡眠状态,然后切换到菜单状态。正如Ben在他的回答中所说的那样,你可以使用if(gameTime.TotalGameTime.TotalMilliseconds > 2300)来执行这个条件,一旦条件被触发,游戏就会更新。

我认为最好解释为什么它被破坏而不是如何解决它。