我目前正在用 Java 中的 Slick2D 开发一个简单的多人游戏。菜单结构会稍微复杂一些,因为我需要几个文本字段来获取连接哪个服务器地址、允许玩多少玩家、昵称等信息。
我在 3 个类中重现了我的问题:主类 ( )ExampleGame
从StateBasedGame
和从扩展。Play
Menu
BasicGameState
这些类每个都有一个文本字段和一个按钮Play
。Menu
我的问题是我只能在文本字段中输入这些类之一。单击文本字段时,另一个文本字段不显示任何光标响应。
这是主类 ExampleGame:
public class ExampleGame extends StateBasedGame {
public static final int MAIN_MENU_STATE = 0;
public static final int GAME_PLAY_STATE = 1;
public ExampleGame(String name) {
super(name);
this.addState(new Menu(MAIN_MENU_STATE));
this.addState(new Play(GAME_PLAY_STATE));
this.enterState(MAIN_MENU_STATE);
}
/**
* @param args
*/
public static void main(String[] args) {
AppGameContainer appgc;
try{
appgc = new AppGameContainer(new ExampleGame("ExampleGame"));
appgc.setDisplayMode(640, 360, false);
appgc.setTargetFrameRate(50);
appgc.start();
}catch(SlickException e){
e.printStackTrace();
}
}
@Override
public void initStatesList(GameContainer arg0) throws SlickException {
// TODO Auto-generated method stub
this.getState(GAME_PLAY_STATE);
this.getState(MAIN_MENU_STATE);
System.out.println(this.getStateCount() + ", " + this.getCurrentStateID());
this.enterState(MAIN_MENU_STATE);
}
}
ExampleGame 自动调用 MainMenu (Menu.java):
public class Menu extends BasicGameState {
private int stateID;
private Image background;
private UnicodeFont font;
private TextField textfield;
private Image createGameOption;
private int menuX = 50;
private int menuY = 250;
public Menu(int stateID) {
this.stateID = stateID;
}
@Override
public void init(GameContainer arg0, StateBasedGame arg1) throws SlickException {
System.out.println("StateID: " + arg1.getState(this.getID()) + " , State: " + arg1.getCurrentState());
background = new Image("images/background_1024x640_v1.jpg");
createGameOption = new Image("images/btn_CreateGame_for_1024x640_v1.png");
textfield = new TextField(arg0, arg0.getDefaultFont(), 100, 100, 200, 30);
}
@Override
public void render(GameContainer arg0, StateBasedGame arg1, Graphics arg2) throws SlickException {
// TODO Auto-generated method stub
background.draw(0, 0);
createGameOption.draw(menuX, menuY);
textfield.render(arg0, arg2);
}
@Override
public void update(GameContainer arg0, StateBasedGame arg1, int arg2) throws SlickException {
Input input = arg0.getInput();
int mouseX = input.getMouseX();
int mouseY = input.getMouseY();
// if mouse is over a button
if ((mouseX >= menuX && mouseX <= menuX + createGameOption.getWidth()) && (mouseY >= menuY && mouseY <= menuY + createGameOption.getHeight())) {
if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
// fx.play();
arg1.enterState(ExampleGame.GAME_PLAY_STATE);
}
}
}
@Override
public int getID() {
// TODO Auto-generated method stub
return stateID;
}
}
在这里,我可以在文本字段中书写。到目前为止,一切都很好。通过单击按钮,我调用了下一页(Play.java):
public class Play extends BasicGameState {
private int stateID;
private Image background;
private UnicodeFont font;
private TextField textfield;
private Image createGameOption;
private int menuX = 350;
private int menuY = 250;
public Play (int stateID) {
this.stateID = stateID;
}
@Override
public void init(GameContainer arg0, StateBasedGame arg1) throws SlickException {
System.out.println("StateID: " + arg1.getCurrentStateID() + " , State: " + arg1.getCurrentState());
arg1.inputStarted();
background = new Image("images/background_1024x640_v1.jpg");
createGameOption = new Image("images/btn_CreateGame_for_1024x640_v1.png");
textfield = new TextField(arg0, arg0.getDefaultFont(), 150, 100, 200, 30);
}
@Override
public void render(GameContainer arg0, StateBasedGame arg1, Graphics arg2) throws SlickException {
// TODO Auto-generated method stub
background.draw(0,0);
createGameOption.draw(menuX, menuY);
textfield.render(arg0, arg2);
}
@Override
public void update(GameContainer arg0, StateBasedGame arg1, int arg2) throws SlickException {
Input input = arg0.getInput();
int mouseX = input.getMouseX();
int mouseY = input.getMouseY();
if ((mouseX >= menuX && mouseX <= menuX + createGameOption.getWidth())
&& (mouseY >= menuY && mouseY <= menuY + createGameOption.getHeight())) {
if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
arg1.enterState(ExampleGame.MAIN_MENU_STATE);
}
}
}
@Override
public int getID() {
// TODO Auto-generated method stub
return stateID;
}
}
该类Play
具有几乎相同的代码,Menu
但似乎文本字段Play
不响应鼠标和键。它似乎处于非活动状态,因为您看不到任何更改。但事实并非如此。如果我在文本字段中单击,输入一些内容并通过单击按钮返回菜单,您可以看到您输入的文本。似乎两个文本字段(来自 Menu 和 from 的那个Play
)都以某种方式连接到。但我不知道问题出在哪里。
有没有人建议我如何解决这个问题?
提前致谢!