0

我在 Game1 类的 switch 块中收到这三个错误消息,但我不知道如何修复它们。怎么了?当前上下文中不存在名称“IntroState” 当前上下文中不存在
名称“MenuState” 当前上下文中不存在名称“MaingameState”

此外,我在 Intro 类中收到此错误消息:当前上下文中不存在名称“IntroState”

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

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


    public void ChangeGameState(GameStates newState)
    {
        switch (newState)
        {
            case IntroState:
                currentState = new Intro(this);
                break;
            case MenuState:
                currentState = new Menu(this);
                break;
            case MaingameState:
                currentState = new Maingame(this);
                break;
        }
        currentState.Load(Content);
    }


    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
{
    Texture2D Introscreen;
    private Game1 game1;       

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

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

    public void Update(GameTime gametime)
    {
        KeyboardState kbState = Keyboard.GetState();
        if (kbState.IsKeyDown(Keys.Space))
          game1.ChangeGameState(IntroState);
    }

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

3 回答 3

1

尝试:

switch (newState)
{
            case GameStates.IntroState:
                currentState = new Intro(this);
                break;
            case GameStates.MenuState:
                currentState = new Menu(this);
                break;
            case GameStates.MaingameState:
                currentState = new Maingame(this);
                break;
 }

IntroState,MenuState并且MaingameState属于一个枚举

于 2012-12-14T23:58:15.250 回答
1

enum只有以类型名称为前缀时才能使用值:

  //case IntroState:
    case GameStates.IntroState:

你永远不能单独使用IntroState

于 2012-12-14T23:58:50.890 回答
1

您应该IntroState称为GameStates.IntroState. 同样适用于其他两个。

于 2012-12-14T23:58:56.803 回答