3

i have a image that's the pause menu. it has an exit button. when the exit button is pressed, a dialog appears. this is what i want.

so, i have an image, and i set the x and y position of where the mouse should be clicked so then the dialog will appear. i want to do something like:

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

    Input input = gc.getInput();
    int xpos = Mouse.getX();
    int ypos = Mouse.getY();


    if ((xpos > 200 && xpos < 250) && (ypos > 230 && ypos < 260)) {

        if (input.isMousePressed(0)) {
            g.drawImage("res/Exit Confirmation.png", 200, 400)
        }

I know the code above cannot be like that. but is there a way that is similar to that?


Here is the code:

import org.lwjgl.input.Mouse;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

public class Menu extends BasicGameState {

public Menu(int state) {

}

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


}

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

    Image background = new Image("res/Background.png");
    g.drawImage(background, 0, 0);

    Image play = new Image("res/Play Button.png");
    g.drawImage(play, 275, 50);

    Image exit = new Image("res/Exit Button.png");
    g.drawImage(exit, 210, 250);

}

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

    Input input = gc.getInput();
    int xpos = Mouse.getX();
    int ypos = Mouse.getY();


    if ((xpos > 300 && xpos < 510) && (ypos > 230 && ypos < 260)) {

        if (input.isMousePressed(0)) {
            sbg.enterState(2); //this will take me to the game.
        }

        if ((xpos > 200 && xpos < 250) && (ypos > 230 && ypos < 260)) {
            //i want this to actually show up a confirmation dialog with my image.
        }
    }
}

public int getID() {
    return 1;
}

}

Please help. Thanks.

Maquaree
  • 43
  • 7
  • You need to encapsulate your button in its own class, and then have the button be responsible for detecting its own hits with an event listener. If possible, make the button draw itself, too, and if not, at least make it keep track of its own location and graphics. If you want the button to do something, just make a new class that extends your button class with the action you want it to perform. – AJMansfield May 20 '13 at 13:33
  • thank u. i am not really good at `slick` - just learning still. it may take me a while to understand all the `slick` things. but i'll get there. – Maquaree May 22 '13 at 13:19

1 Answers1

2

Ok, it appears you have several conceptual problems. Let me see if I can help.

  1. First, you don't want to load images on the render() method. That's what init() is for. That way, you only load them once, not hundreds of times per second.
  2. The confirmation dialog will actually be there for many frames, so it will be rendered many times, independently on the instant in which it appeared. So it needs to be shown with the click but it's state remembered.
  3. You probably want to catch the click once, not keep it down without being able to move the mouse. So instead of asking for the mouse every update, use the events.

Here is the code (although I didn't test it, but should be close):

import org.lwjgl.input.Mouse;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

public class Menu extends BasicGameState {

Image background;
Image play;
Image exit;
Boolean exiting;

public Menu(int state) {
    exiting = false;
}

public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
    Image background = new Image("res/Background.png");
    Image play = new Image("res/Play Button.png");
    Image exit = new Image("res/Exit Button.png");
}

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

    g.drawImage(background, 0, 0);
    g.drawImage(play, 275, 50);

    if(exiting)
        g.drawImage(exit, 210, 250);
}

@Override
public void mousePressed(int button, int x, int y)
{
    if( button == 0 )
    {
        if ((x > 300 && x < 510) && (y > 230 && y < 260))
            sbg.enterState(2); //this will take me to the game.
        else if ((x > 200 && x < 250) && (y > 230 && y < 260))
            exiting = true;
    }
}

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

}

public int getID() {
    return 1;
}

}
Daniel Langdon
  • 5,899
  • 4
  • 28
  • 48
  • In the function mousePressed(int button, int x, int y) are you getting the sbg object? – Trevi Awater Nov 22 '14 at 15:55
  • Uh, sorry. I basically re-arranged the original code to put things on the correct place. That's why I mentioned the solution would be "close". Of course, you would need to make it compile, which probably means accessing sbg somehow, either as a class variable, access to a static or singleton object, etc ;-) – Daniel Langdon Nov 25 '14 at 13:27