0

我在将 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);
}

在 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 我有我的平局

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);
    }
}

还有我的 Playing.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 不会出现。

4

1 回答 1

0

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

但是,对一个对象有多个引用是可以的。Game

Game在最顶层保留一个对象;您已经拥有它来保存您的屏幕对象。您甚至可以将mainandmainG变量保留在屏幕中,但让它们指向您的单Game

public class SplashScreen
{
    Game1 main;
    public SplashScreen(Game1 g)
    {
        main = g;
    }

    //rest can be largely the same
}

对 Playing 执行基本相同的操作(接受 aGame1并设置引用而不是重新声明)。

然后在 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 示例以帮助您入门。

于 2013-04-01T14:10:59.823 回答