Cant draw when using a different enum gamestate state

I’m making a supermario style 2D scroller game for a uni class, and for some reason the spritebatch Draw method does not draw when i change my enum state.


Here’s my enum with my different gamestates

The draw method in the main class game1

I’ve tried different states but in this case im using leveleditor

The drawleveleditor method in my Gamestatehandler class


The draw method in my leveleditor class. I’ve tried using an overload with layerdepth as well with no success.

The backgrounds.draw method is used when in gamestate game as well, and it works perfectly there. But I can’t seem to draw anything using my other gamestates. It doesn’t throw an error either, and my texture and rectangle are correct. It also enters all the correct methods when looping through the code.

Couldnt add it to the post as I’m a new user and not allowed more than 4 screenshots, but I do set my gamestate to leveleditor in the LoadContent method of the main class, and I¨ve quadruple checked if it goes through all the methods correctly

1 Like

I’m sorry, I don’t know what you’re referring to that could help me in that article? I read through it, but I believe my state implementation is not the issue, and I’ve worked the same way in previous projects. Thanks for your response though!

SOLVED: Forgot to Update the camera lol

Just to show you another approach, I guess :slight_smile:
The ever expanding switch statement can get out of hand really quickly.
Implementing a state class is easy and great for expanding AND maintainance.

PS: you can use a named parameter in the spriteBatch begin

1 Like

Yes, 100%!

I’ve fallen into this before. In fact, I went digging through my old projects and found an Update loop with 151 lines of switch statement doing different things based on state. I was going to post it but I figured I’d spare everybody the experience :wink:

Ultimately though, coding is a journey and you learn as you go. What @KingWille has here will work fine, it’s just good to be aware that there are other ways.

There are a lot of ways you could go here, such as creating an actual scene management system, but a simplified alternative that at least makes the code more readable might be something like this…

public class IStateHandler
  void Update(GameTime gameTime);
  void Draw(GameTime gameTime);

// Handler classes that implement IStateHandler and do what you normally do
public class StartStateHandler : IStateHandler { ... }
public class GameStateHandler : IStateHandler { ... }
// ... and so on ...

public class MainGame : Game
  public Dictionary<GameState, IStateHandler> _stateHandlers = new ...
  public GameState _state = GameState.start;

  protected override void Initialize()
    _stateHandlers.Add(GameState.start, new StartStateHandler(...));
    _stateHandlers.Add(, new GameStateHandler(...));
    // ... and so on ...

    // whatever else is going on

  protected override void Draw(GameTime gameTime)

  // Same idea for Update, managing state transitions as you already do.

As I said, I’m not claiming this is the only way or anywhere approaching the best way, it’s just another option. I also wouldn’t worry about the code you have right now, maybe just keep this in mind. Especially if you find yourself adding more states in the future.

I’ll look into this in the future! Thank you!