我目前正在使用Java中的Slick2D开发一个简单的多人游戏。菜单结构会有点复杂,因为我需要几个文本字段来获取连接服务器地址的信息,允许播放的玩家数量,昵称等等。
我在3个课程中重现了我的问题:主要课程(ExampleGame
)来自StateBasedGame
的{{1}},Play
和Menu
。
班级BasicGameState
和Play
分别有一个文字字段和一个按钮。我的问题是我只能将其中一个类输入到文本字段中。单击文本字段时,另一个文本字段不显示任何光标响应。
这是主要的类ExampleGame:
Menu
}
ExampleGame自动调用MainMenu(Menu.java):
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);
}
}
我可以在这里写一下文字字段。到现在为止还挺好。通过单击按钮,我调用下一页(Play.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;
}
}
类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
的文本字段)都连接到不知何故。但我不知道问题出在哪里。
有没有人建议我如何解决这个问题?
提前致谢!
答案 0 :(得分:0)
这是你正在寻找的确切答案,我不久前就回答了。此用户遇到了与您相同的问题,并通过关于如何使用StateBasedGame的示例关注我的wiki链接来解决此问题