gpt4 book ai didi

java - Slick2D 更新文本/点击选项

转载 作者:行者123 更新时间:2023-12-01 11:40:41 25 4
gpt4 key购买 nike

嘿,我正在 slick2D 中开发一个小项目,我正在尝试添加玩家和 npc 之间的交互,您可以在其中单击以选择回复。

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

运行游戏时:当与npc对话msgID变成1时,那么玩家有2个选择“俄罗斯娃娃”和“不”。单击“俄罗斯娃娃”应该会将用户带到下一条消息 (msgID = 2),其中用户可以回复“有趣”,这会将他们带回 msgID = 0。

我的问题是,当单击“俄罗斯娃娃”时,更新方法会重新运行,从而立即选择“有趣”。我试图修复它,但没有成功,当我选择“俄罗斯娃娃”时,系统通过单击输出(“1, 4”)。

单击第一个选项会选择第一个选项两次,因此将播放器返回到原始状态。似乎无法修复:/

最佳答案

所以这里发生的事情是,当鼠标按钮按下时,它会获取输入,因此当鼠标按钮按下时,它将执行所涉及的所有内容。这里你需要做的不仅仅是按下鼠标,还需要使 if 语句像这样。只是为了让您知道这是 sudo 代码。

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

在您的代码中尝试类似的操作。

关于java - Slick2D 更新文本/点击选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29552256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com