LongInt
LongInt

Reputation: 1779

Render is not called in a BasicGameState

I am starting a very simple game in java, but am already stuck at the first step. I've made a StateBasedGame class (Kreations), which succesfully adds and enters and inits my BasicGameState (MenuGameState). However the update() and render() methods of MenuGameState are not called - and I have trouble figuring out why. Can you help? Here follows the code:

StateBasedGame:

import gamestates.MenuGameState;

import org.newdawn.slick.*;
import org.newdawn.slick.state.StateBasedGame;

public class Kreations extends StateBasedGame
{

  public Kreations() {
        super("Kreations");
    }

  @Override
  public void update(GameContainer gc, int delta) throws SlickException
  {

  }

  @Override
  public void render(GameContainer gc, Graphics g) throws SlickException
  {

  }

  public static void main(String[] args) throws SlickException
  {
      try { 
           AppGameContainer container = new AppGameContainer(new Kreations()); 

           container.setDisplayMode(800,600,false); 

         //Make sure the logic updates consistently
           container.setMinimumLogicUpdateInterval(20);
           container.setMaximumLogicUpdateInterval(20);

           container.start();
         } catch (SlickException e) { 
           e.printStackTrace(); 
         }
  }

    @Override
    public void initStatesList(GameContainer gc) throws SlickException {
        this.addState(new MenuGameState());
    }
}

BasicGameState:

package gamestates;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

public class MenuGameState extends BasicGameState {

    public static final int ID = GameStates.MAIN_MENU.ordinal();

    @Override
    public void init(GameContainer gc, StateBasedGame sbg)
            throws SlickException {

    }

    @Override
    public void render(GameContainer gc, StateBasedGame sbg, Graphics g)
            throws SlickException {
        g.drawString("Hello World", 100, 100);
    }

    @Override
    public void update(GameContainer gc, StateBasedGame sbg, int delta)
            throws SlickException {

    }

    @Override
    final public int getID() {
        // TODO Auto-generated method stub
        return GameStates.MAIN_MENU.ordinal();
    }

    public void enter(GameContainer container, StateBasedGame game) throws SlickException {
          System.out.print("MenuGameState.enter() is called");
       }

}

Upvotes: 0

Views: 169

Answers (2)

JB Nizet
JB Nizet

Reputation: 691953

I haven't found the javadoc of the StateBasedGame class, but my guess is that the current state is rendered by the method render() of StateBaseGame. And since you overrode the method to make it do nothing, your state's render() method is never called by any object. Try removing the overridden render() and update() method in your main class.

BTW, in the source file that I found on github, these two methods are final, so it's impossible to override them, which probably indicates that these methods are not supposed to be overridden.

Upvotes: 0

Jesse Jashinsky
Jesse Jashinsky

Reputation: 10663

Unless there's code elsewhere that's supposed to call those methods, update() and render() methods of MenuGameState are not called because there are no calls to them.

Upvotes: 1

Related Questions