我目前正在用Java中的Slick2D開發一個簡單的多人遊戲。菜單結構會稍微複雜一點,因爲我需要幾個文本框來獲取服務器地址連接的信息,有多少玩家可以玩,暱稱等等。帶有BasicGameState的Slick2D中的Textfield不回答
我轉載了我的問題在3類:主類(ExampleGame
)從StateBasedGame
,從BasicGameState
Play
和Menu
延伸。
類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
文本字段不響應鼠標和鍵。它似乎是無效的,因爲你看不到任何變化。但事實並非如此。如果我在文本框中單擊,鍵入內容並通過單擊按鈕返回到菜單,您可以看到您鍵入的文本。似乎這兩個文本框(來自菜單和Play
)都以某種方式連接。但我不知道問題出在哪裏。
有沒有人有任何建議,我可以解決這個問題?
在此先感謝!
您是否確定它是同一個問題?我試圖按照你的建議改變我的SBG,但它已經非常相似。沒有骰子。 Slick2d實際上能夠處理多個文本字段嗎? – AAAton 2014-05-08 14:58:32