Andy
Andy

Reputation: 55

'GameStates': cannot reference a type through an expression; try 'menuinterface.Game1.GameStates' instead

I changed my code but it still doesn't work. I get this error message in the Intro class: 'GameStates': cannot reference a type through an expression; try 'menuinterface.Game1.GameStates' instead

What is wrong? I want to set the gamestate to MenuState if the player presses the Space Key.
Game1 class:

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

Interface:

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

Intro class:

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

Upvotes: 0

Views: 2325

Answers (2)

Hoeloe
Hoeloe

Reputation: 650

Your problem is not understanding how enumerative types work. Specifically, this line:

game1.GameStates = IntroState;

First, let's discuss a little of what an enum actually is. It's a structure that simply assigns names to integer values, and can then refer to each value by name, and thus make code more readable (as it's far easier to understand direction = Dir.Up than direction = 1).

Notice how I use those two example statements, though. In the code, you treat the enum as a type not as a variable, and this is the problem you're encountering. In fact, your problem is twofold. The first issue id that you're trying to assign a value to a structure, which is similar to writing int = 4 - i.e. it doesn't make sense. Your second issue is that enums are not global, so IntroState has no meaning outside game1.

Interestingly enough, you've set up the system correctly, as you have the currentGameState variable, which is what you actually intend to change. However, it is a private variable, disallowing access to it from outside the game1 class. You can either make the variable public, or create a property to access it. The latter is good practice, as it allows you to control how the variable is set. To create this, you can use something like this:

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

Once you have this in your game1 class, you can set the game state like so:

game1.CurrentState = Game1.GameStates.IntroState;

Notice how this code tells the program where to look for what you want. IntroState is part of a structure (GameStates) within game1, and so needs to be accessed explicitly.

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.

Upvotes: 4

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174427

Do what the error message says:

game1.GameStates = Game1.GameStates.IntroState;

You need to fully qualify that enum value.

Upvotes: 0

Related Questions