Question

I am trying to create a state based game in SLick2D and faced a difficulty while trying to create various events in the same class. I want to display an image on the screen and when the player clicks, the image gets replaced by another one.

I don't know how to do this since the command of the clicking is in the 'update' method, and the images get displayed in the 'render'. So basically, I don't know how to return to my render method after using the update method. Here's a pseudo of what I tried to do. Still I can't figure out how to do it.

private int a = 0;

public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
Image a = new Image("blabla"); //create 2 images
Image b = new Image("blabla");
if (a==0){            //when a=0 draw the first image
g.drawImage(a,0,0);
}
if (a==1){          //when a=1 draw another image, but only after mouse is clicked (a is changed to 1 when mouse is clicked in update method)
g.drawImage(b,0,0);
}


public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{  
Input input = gc.getInput();
int xpos = Mouse.getX();//222
int ypos = Mouse.getY();

if((xpos>00 && xpos<50) && (ypos>0 && ypos<222)){ // when user clicks set a to 1
        if(input.isMouseButtonDown(0)){
            a++;
            somehow return to render method;
        }

PS This is my first year in Java so don't laugh at me and use complicated terms :)

Was it helpful?

Solution

First, in your render method, you call:

Image a = new Image("blabla"); //create 2 images
Image b = new Image("blabla");

With this, you are creating new Image objects every time that render gets called. So what you'd need to do is create fields to hold your images and initialize them in the init(...) method. Here's an example:

public class TestGame extends StateBasedGame {

    Image a;
    Image b;

    public void init(GameContainer container, StateBasedGame game) 
            throws SlickException {
        a = new Image("blabla1.png");
        b = new Image("blabla2.png");
    }

    public void update(GameContainer container,
                       StateBasedGame game,
                       int delta) throws SlickException {
        // Put your update stuff here
    }

    public void render(GameContainer container,
                       StateBasedGame game,
                       Graphics g) throws SlickException {
        if (a == 0) {
            g.drawImage(a, 0, 0);
        } else if (a == 1) {
            g.drawImage(b, 0, 100);
        }
    }
}

Now back to the question, you don't need to worry about 'returning to render' because update and render are kind of like helper methods in an infinite game loop. So what you change in update or render will be seen in either (unless you are changing local variables). Here's a rough pseudo-representation of what the game loop would look like:

while (!gameCloseRequested) {
    update();
    render();
}

So given that, update and render are continuously called, which means you never have to "jump" from one method to another.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top