Имя IntroState не существует в текущем контексте.

Я получаю эти три сообщения об ошибках в блоке переключателей класса Game1, но не знаю, как их исправить. Что случилось? Имя '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);
    }
}

3 ответа

Решение

enum Значение может использоваться только в том случае, если в качестве префикса используется имя типа:

  //case IntroState:
    case GameStates.IntroState:

ты никогда не сможешь использовать IntroState сам по себе.

Вы должны обратиться к IntroState как GameStates.IntroState, То же относится и к двум другим.

Пытаться:

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 принадлежит перечислению

Другие вопросы по тегам