Init, Render and Update [FIXED]

Hello guys, Im doing some tests and i founded something interesting. Probably Im doing something wrong, but i cant find it.

I have a “test game” with 2 states: MainMenu and GamePlay.

I will copy just the basic of the code:

public class GamePlayState extends BasicGameState {

    int stateID;
    private boolean escape_pressed;
	
    public GamePlayState( int stateID ) {
       this.stateID = stateID;
    }
 
    @Override
    public int getID() {
        return stateID;
    }
 
    
    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
		
		escape_pressed= false;
		
		gc.getInput().addKeyListener(listener);
    }
 
    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
    	
       //here i draw things bla bla...
    			
    			
    }
 
    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {		
		if(escape_pressed) sbg.enterState(GameTest08.MAINMENU_STATE);
		
		player.update(delta);

    }//fin update

And the MainMenu

public class MainMenuState extends BasicGameState {

    int stateID;
    
    private Image background;
    
    private long elapsedTime;
    private boolean showHelp;
    
    public MainMenuState(int ID){
    	this.stateID= ID;
    }
    
   
    public int getID() {
        return stateID;
    }

	
	public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
		background= new Image("data/tests/fondo2.jpg");
		elapsedTime= 0;
		showHelp= false;
		
	}

	
	public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
		background.draw(0, 0, gc.getWidth(), gc.getHeight());
		
		g.drawString(String.format("blablabla this is the menu"), 500, 400);
		
		if(showHelp) g.drawString(String.format("blablablabla help"), 510, 450);
		
	}

	
	public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
		
		if(gc.getInput().isKeyDown(Input.KEY_ENTER)) sbg.enterState(GameTest08.GAMEPLAY_STATE);
		
		elapsedTime+= delta;
		if(elapsedTime >= 5000) showHelp= true;
		
	}
    
}//fin de clase

The thing is, on GamePlay Im using a class “Escuchador” who implements keyListener. When you are on MainMenu, you press ENTER and GamePlay starts. All good. (Look at “elapsedTime” variable…on init, its 0.)

Now we are on GamePlay. The keyListener works very well and when i press ESCAPE, escape_pressed= true. (Look at the init, escape_pressed= false). On the update, as escape_pressed==true, we go back to the menu.

Again on MainMenu, elapsedTime should be 0. Its not. “help” its visible becouse elpasedTime have the same value has before enter GamePlay. Also, now I press ENTER to go to GamePlay again. Right after entering GamePlayState, I come back to MainMenu, of course, escape_pressed is true. Conclusion: init method does not run when I enter on a new gameState. Is that normal?

FIXED

I realize that i must implement the “public void enter(…)” method. This one is executed everytime I enter on a new state.

:slight_smile: