How to correctly load a state? [SOLVED]

So game progress has been going quite nicely:

LzLgQsTy0V0

But I’ve noticed something – the actual game’s backgroundmusic and the heartbeat sound plays in the menu state.

So I’ve tried to make all the stuff for the actual game (such as the backgroundmusic and the heartbeat) init only if the game is in the play(actual game) state (which is 2).

Play.java:

if(sbg.getCurrentStateID() == 2){
			
			// Images
			map = new Image("resources/Images/Backgrounds/backGround_two.png");

			// Music
			backGroundMusic = new Sound("resources/Music/BGM/eerie.wav");
			backGroundMusic.loop();
			// HUD BLOCK START
			hudGUI = new HUD();

			healthGUI = new Health();
			healthGUI.init(gc);

			inventoryGUI = new Inventory();
			inventoryGUI.init(gc);
			// HUD BLOCK END

			mainPlayer.getWeapon().init(gc);
			
		}

But that doesn’t work.

So I’ve made a breakpoint in the Menu’s update method, and a breakpoint for that if statement in the play class – debugged – the play class’ if statement is met first
and THEN the update method is met.

Quite strange.

Here’s how the “main game” (not the “actual game state”, which is Play.java, sorry if all these names are confusing :S) class is setup:

Game.java:

public void initStatesList(GameContainer container) throws SlickException {

		// 0
		ResourcesState rs = new ResourcesState();
		states.add(rs);
		addState(rs);

		// 1
		Menu menu = new Menu();
		states.add(menu);
		addState(menu);
		
		// 2
		Play play = new Play();
		states.add(play);
		addState(play);
		
		// 3
		Credit credits = new Credit();
		states.add(credits);
		addState(credits);
		
	}

Any suggestions?

I’d place the backGroundMusic.loop in the enter() method, and put some sort of pause in the leave() method of the play state.

A little more of your code would be useful, though.

There is no “enter()” method nor is there a “leave()” method.

Unless you’re referring to enter() as the init() method.

If so, it is in that method.

From the looks of it, you are using Slick2D’s StateBasedGame approach. If that’s the case, the BasicGameState class has two classes:

enter(GameContainer gc, StateBasedGame sbg) throws SlickException
leave(GameContainer gc, StateBasedGame sbg) throws SlickException

that can optionally be overwritten. As the names suggest, enter runs when a state is transitioned into, and leave runs when exiting the state.

Oh awesome! Thanks, I’ll be sure to reply with my results :slight_smile:

I did

public void enter(GameContainer gc, StateBasedGame sbg) throws SlickException {
		
		// Images
		map = new Image("resources/Maps/chinatown.png");
		tileManager = new TileManager(map);
		tileManager.addSolidTile(0, 0);

		// Music
		backGroundMusic = new Sound("resources/Music/BGM/eerie.wav");
		backGroundMusic.loop();
		// HUD BLOCK START
		hudGUI = new HUD();

		healthGUI = new Health();
		healthGUI.init(gc);

		inventoryGUI = new Inventory();
		inventoryGUI.init(gc);
		// HUD BLOCK END

		mainPlayer.getWeapon().init(gc);
		magnum = new Magnum(null);
		magnum.init(gc);
		assault = new LE9AR(null);
		assault.init(gc);
		
	}

and it works!

Only plays the Play’s music and the heartbeat sound once you go ingame!

Thanks a lot man :slight_smile: