ScreenState Trouble XNA 4 C#

本文关键字:XNA Trouble ScreenState | 更新日期: 2023-09-27 18:06:41

我在SplashScreen切换到播放状态时遇到了麻烦。我在Game1.cs

中声明了一个游戏状态的枚举
public enum GameState { SplashScreen, Playing }
public GameState currentState;

然后我在Game1.cs

的LoadContent中添加这个
protected override void LoadContent()
{
    // Create a new SpriteBatch, which can be used to draw textures.
    spriteBatch = new SpriteBatch(GraphicsDevice);
    splashScreen.LoadContent(Content);
    playState.LoadContent(Content);
}

In Update In Game1.cs

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:
        {
            splashScreen.Update(gameTime);
            break;
        }
        case GameState.Playing:
        {
            playState.Update(gameTime);
            break;
        }
    }
    base.Update(gameTime);
}

最后在Game1.cs中我有了draw

protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.CornflowerBlue);
    spriteBatch.Begin();
    switch (currentState)
    {
        case GameState.SplashScreen:
        {
            splashScreen.Draw(spriteBatch);
            break;
        }
        case GameState.Playing:
        {
            playState.Draw(spriteBatch) ;
            break;
        }
    }
    spriteBatch.End();
    base.Draw(gameTime);
}

SplashScreen.cs

public class SplashScreen
{
    Texture2D tSplash;
    Vector2 vSplash = Vector2.Zero;
    Game1 main = new Game1();
    public void LoadContent(ContentManager Content)
    {
        tSplash = Content.Load<Texture2D>("splash");
    }
    public void Update(GameTime gameTime)
    {
        if (Keyboard.GetState().IsKeyDown(Keys.X))
        {
            main.currentState = Game1.GameState.Playing;
        }
    }
    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(tSplash, vSplash, Color.White);
    }
}

和我的play .cs

class Playing
{
    Texture2D tBack;
    Vector2 vBack = Vector2.Zero;
    Game1 mainG = new Game1();
    public void Initialize()
    {
    }
    public void LoadContent(ContentManager contentManager)
    {
        tBack = contentManager.Load<Texture2D>("playing");
    }
    public void Update(GameTime gameTime)
    {
    }
    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(tBack, vBack, Color.White);
    }
}

SplashScreen图像显示,但当我按X时,PlatyingState不发生

ScreenState Trouble XNA 4 C#

您正在为每个屏幕声明一个全新的Game对象。千万不要这样做。一个Game代表你的整个游戏。

然而,有多个引用到一个单一的Game对象是可以的。

在最顶层保留单个Game对象;你已经让它保存了屏幕对象。您甚至可以在屏幕中保留mainmainG变量,但让它们指向单个Game:

public class SplashScreen
{
    Game1 main;
    public SplashScreen(Game1 g)
    {
        main = g;
    }
    //rest can be largely the same
}

对Playing做基本相同的操作(采用Game1并设置引用,而不是重新声明)。

然后在Game1.cs中:

protected override void LoadContent()
{
    // Create a new SpriteBatch, which can be used to draw textures.
    spriteBatch = new SpriteBatch(GraphicsDevice);
    splashScreen = new SplashScreen(this);
    splashScreen.LoadContent(Content);
    playState = new Playing(this);
    playState.LoadContent(Content);
}

这将是一个很好的第一步,尽管我建议更进一步,为所有屏幕创建一个基类。查看游戏屏幕管理的官方XNA样本,让你开始。