Search code examples
javaslick2d

Slick2D updating text/click options


Hey so Im working on a small project in slick2D, im trying to add interaction between the player and an npc where you can click to select a reply.

public class House extends BasicGameState {
Image house;
Image message;
int msgID = 0;
...

public House(int state) {

}

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

}

public void render(GameContainer gc, StateBasedGame sbg, Graphics g)
        throws SlickException {

    house.draw(playerPositionX, playerPositionY);
    player.draw(shiftX, shiftY);

    if (exitHouse == true){
        g.drawString("Would you like to exit the house? Y/N", 520, 270);
    }
    if (msgID == 1){
        message.draw(0, 570);
        mollerFace.draw(39, 610);
        ttf.drawString(46,588, "Moller");
        ttf.drawString(180, 609, "'Ello fella, you interested in some russian dolls?");
        ttf.drawString(908, 613, "- Russian dolls?" );//option 1
        ttf.drawString(908, 633, "- No" );//option2
        if (Play.spokeToMarkus){
            ttf.drawString(908, 653, "- Biscuits?" );//option3 hidden
        }
    } else if (msgID == 2){
        message.draw(0, 570);
        mollerFace.draw(39, 610);
        ttf.drawString(46,588, "Moller");
        ttf.drawString(180, 609, "Russian dolls are a set of dolls where each doll is different size, designed to fit in each other.");
        ttf.drawString(180, 636, "I can't recall how it's related to the module but it somehow is! ");
        ttf.drawString(908, 613, "- Interesting" );//option 1
        if (Play.spokeToMarkus){
            ttf.drawString(908, 633, "- Biscuits?" );//option 2 hidden
        }
    }
     else if (msgID == 3){
            message.draw(0, 570);
            mollerFace.draw(39, 610);
            ttf.drawString(46,588, "Moller");
            ttf.drawString(180, 609, "Biscuits? I don't really have any biscuits but I know where to get them from.");
            ttf.drawString(180, 636, "But first I'm dying, get me something to drink and I'll help ya' out");
            ttf.drawString(908, 613, "- Fine" );//option 1
        }
}


public void update(GameContainer gc, StateBasedGame sbg, int delta)
        throws SlickException {
    Input input = gc.getInput();
    // move up
    if (input.isKeyDown(Input.KEY_UP)) {
    up(delta);
    }
    // move down
    if (input.isKeyDown(Input.KEY_DOWN)) {
        down(delta);
    }

    // move left
    if (input.isKeyDown(Input.KEY_LEFT)) {
        left(delta);
    }
    // move right
    if (input.isKeyDown(Input.KEY_RIGHT)) {
        right(delta);
    }

    //speaking to moller

    if (msgID == 1){
        //option 1
        if ((Mouse.getX() > 907 && Mouse.getX()< 1023) 
                && (Mouse.getY() > 88 && Mouse.getY() < 107)) {
            if (input.isMouseButtonDown(0)) {
                msgID = 2;

                System.out.println("1");
            }
        }
        //option 2
        if ((Mouse.getX() > 907 && Mouse.getX()< 940) 
                && (Mouse.getY() > 69 && Mouse.getY() < 83)) {
            if (input.isMouseButtonDown(0)) {
                msgID = 0;
                System.out.println("2");
            }
        }
        //option 3
        if ((Mouse.getX() > 907 && Mouse.getX()< 982) 
                && (Mouse.getY() > 46 && Mouse.getY() < 66)) {
            if (input.isMouseButtonDown(0)) {
                if(Play.spokeToMarkus){
                msgID = 3;
                System.out.println("3");
                }
            }
        }
    }
    if (msgID == 2){
        //option 1
        if ((Mouse.getX() > 907 && Mouse.getX()< 1023) 
                && (Mouse.getY() > 88 && Mouse.getY() < 107)) {
            if (input.isMouseButtonDown(0)) {
                msgID = 0;
                System.out.println("4");
            }
        }
        //option 2
        if ((Mouse.getX() > 907 && Mouse.getX()< 983) 
                && (Mouse.getY() > 69 && Mouse.getY() < 83)) {
            if (input.isMouseButtonDown(0)) {
                if(Play.spokeToMarkus){
                    msgID = 3;
                    System.out.println("5");
                }
            }
        }
    }
     if (msgID == 3){
        //option 1
        if ((Mouse.getX() > 907 && Mouse.getX()< 1023) 
                && (Mouse.getY() > 88 && Mouse.getY() < 107)) {
            if (input.isMouseButtonDown(0)) {
                msgID = 0;
                System.out.println("6");
            }
        }
    }

while running the game: when speaking to the npc msgID becomes 1, then the player has 2 options "Russian dolls" and "no". clicking "russian dolls" is supposed to take the user to the next message (msgID = 2), in which the user can reply with "interesting" which takes them back to msgID = 0.

My problem is when clicking "russian dolls", the update method is re-run and thus selects "interesting" instantly. I tried to fix it but had no success, when I select "russian dolls", the system outputs("1, 4") from a single click.

Clicking on the first option selects the first option twice and therefore returns the player to the original state. Can't seem to fix:/


Solution

  • So what is happening here is that it takes the input of the while the mouse button is down so while the mouse button is down it will execute everything it is involved with. What you need to do here is have it not just be a mouse down but you need to make the if statement something like this. Just so you know this is sudo code.

    boolean isMouseButtonDown = false;
    
    if(inputForMouseButton == true && isMouseButtonDown == false && msgID == 2){
       //do what ever is in the if statement for going to the next chat
       isMouseButtonDown = true;
    }
    
    if(inputForMouseButton == false && isMouseButtonDown == true){
        isMouseButtonDown = false;
    }
    

    Try something like this in your code.