有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用slick2d mouselistener来使用这个?

我有一个图像是暂停菜单。它有一个退出按钮。按下退出按钮时,会出现一个对话框。这就是我想要的

所以,我有一个图像,我设置了鼠标应该点击的x和y位置,这样对话框就会出现。我想做一些类似的事情:

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)
        }

我知道上面的代码不能是那样的。但有没有类似的方法


代码如下:

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;
}

}

请帮忙谢谢


共 (1) 个答案

  1. # 1 楼答案

    好吧,看来你有几个概念上的问题。让我看看能不能帮上忙

    1. 首先,您不想在render()方法上加载图像。这就是init()的用途。这样,你只需加载一次,而不是每秒数百次
    2. 确认对话框实际上会在那里出现很多帧,因此它会在出现的那一刻被单独渲染多次。所以它需要通过点击来显示,但它的状态被记住了
    3. 你可能想抓住点击一次,而不是在无法移动鼠标的情况下按住它。因此,与其每次更新都要求鼠标,不如使用事件

    下面是代码(虽然我没有测试它,但应该很接近):

    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;
    }
    
    }