0

我试图让我的 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);
        }
    }
}
4

2 回答 2

2

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

于 2013-03-25T11:00:42.403 回答
0

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

它总是进入菜单而不是你的飞溅的原因是因为Thread.Sleep(2300).

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

我认为最好解释它为什么坏了,而不仅仅是如何修复它。

于 2013-07-01T00:25:27.633 回答