Search code examples
javaimageuser-interface2dslick2d

How do you toggle an image with a button event in Slick?


So I am trying to get a simple GUI setup for my teams game we are starting and I'm trying to make a drop down inventory using "I" as the hot key for it and it drops fine but I can't seem to figure out a way without using a different key to make it retract or in essence "un" draw the image'

public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws         
    SlickException {
    mapHud.draw(440, 1);
    hotBar.draw(160, 454);
    if(inv){
        inventory.draw(-40, 1);
    }
}

@Override
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws    SlickException {
    Input input = gc.getInput();
    if(input.isKeyPressed(Input.KEY_I)){
        inv = true;
    }
    if(input.isKeyPressed(Input.KEY_ESCAPE)) {
        sbg.enterState(0);
    }
}

Solution

  • Try something like this:

    if(input.isKeyPressed(Input.KEY_I)){
     inv = !inv;
    }