3

ポーズメニューの画像があります。終了ボタンがあります。終了ボタンを押すと、ダイアログが表示されます。これは私が欲しいものです。

だから、私は画像を持っていて、マウスをクリックする必要がある場所の 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;
}

}

助けてください。ありがとう

4

1 に答える 1

2

わかりました、いくつかの概念上の問題があるようです。お手伝いできるかどうか見てみましょう。

  1. まず、render() メソッドで画像をロードしたくありません。それが init() の目的です。そうすれば、1 秒間に何百回も読み込むのではなく、1 回だけ読み込むことができます。
  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;
}

}
于 2013-05-21T05:41:35.710 に答える