0

我更改了我的代码,但它仍然无法正常工作。我在 Intro 类中收到此错误消息:“GameStates”:无法通过表达式引用类型;尝试“menuinterface.Game1.GameStates”

怎么了?如果玩家按下空格键,我想将游戏状态设置为 MenuState。
游戏1类:

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    private IState currentState;

    public enum GameStates
    {
        IntroState = 0,
        MenuState = 1,
        MaingameState = 2,
    }

    public GameStates CurrentState
    {
        get { return currentGameState; }
        set { currentGameState = value; }
    }

    private GameStates currentGameState = GameStates.IntroState;


    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }

    protected override void Initialize()
    {
        currentState = new Intro(this);
        base.Initialize();
    }

    protected override void LoadContent()
    {       
        spriteBatch = new SpriteBatch(GraphicsDevice);
        currentState.Load(Content);
    }

    protected override void Update(GameTime gameTime)
    {
        currentState.Update(gameTime);     
        KeyboardState kbState = Keyboard.GetState();
        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
        spriteBatch.Begin();
        currentState.Render(spriteBatch);
        spriteBatch.End();

        base.Draw(gameTime);
    }
}

界面:

public interface IState         
{ 
        void Load(ContentManager content); 
        void Update(GameTime gametime); 
        void Render(SpriteBatch batch); 
} 

介绍班:

public class Intro : IState
{
    private IState currentState;
    Texture2D Menuscreen;
    private Game1 game1;       

    public Intro(Game1 game)
    {
        game1 = game;
    }

    public void Load(ContentManager content)
    {
       Menuscreen = content.Load<Texture2D>("menu");
    }

    public void Update(GameTime gametime)
    {
        KeyboardState kbState = Keyboard.GetState();
        if (kbState.IsKeyDown(Keys.Space))
            game1.CurrentState = game1.GameStates.IntroState;
          currentState = new Menu(game1);
    }

    public void Render(SpriteBatch batch)
    {
        batch.Draw(Menuscreen, new Rectangle(0, 0, 1280, 720), Color.White);
    }
}
4

2 回答 2

4

您的问题是不了解枚举类型的工作原理。具体来说,这一行:

game1.GameStates = IntroState;

首先,让我们讨论一下枚举实际上是什么。它是一个简单地将名称分配给整数值的结构,然后可以按名称引用每个值,从而使代码更具可读性(因为它比 更容易理解direction = Dir.Updirection = 1

不过请注意我是如何使用这两个示例语句的。在代码中,您将枚举视为类型而不是变量,这就是您遇到的问题。事实上,你的问题是双重的。您尝试为结构分配值的第一个问题 id 类似于写作int = 4- 即它没有意义。您的第二个问题是枚举不是全局的,因此IntroState在外部没有意义game1

有趣的是,您已经正确设置了系统,因为您拥有currentGameState变量,这正是您真正打算更改的内容。但是,它是一个private变量,不允许从 game1 类外部访问它。您可以创建变量public,也可以创建一个property来访问它。后者是很好的做法,因为它允许您控制变量的设置方式。要创建它,您可以使用以下内容:

public GameStates CurrentState
{
    get { return currentGameState; }
    set { currentGameState = value; }
}

一旦你在你的 game1 类中有这个,你可以像这样设置游戏状态:

game1.CurrentState = Game1.GameStates.IntroState;

注意这段代码是如何告诉程序去哪里寻找你想要的东西的。IntroStateGameStatesgame1 中结构 ( ) 的一部分,因此需要显式访问。

Hopefully, this has helped you to understand how enumerative types work, and why the code you wrote doesn't make sense, and thus why it breaks.

于 2012-12-14T11:58:48.923 回答
0

执行错误消息中的说明:

game1.GameStates = Game1.GameStates.IntroState;

您需要完全限定该枚举值。

于 2012-12-14T11:49:34.663 回答