好的,所以我正在用libgdx开发一个Android /桌面游戏。我最近取得了很多进展,但仍有一个问题。
我有一个if / else块,用于检查用户是否在共享首选项中存储了名称。如果没有,则会弹出一个带有文本输入和按钮的libgdx窗口。单击按钮提交后,将播放器信息发送到服务器,服务器发回高分列表,并构建表格以显示它们。问题是,我现在还没有看到我的桌子。调试线是绘制的,我100%肯定数据会恢复干净,因为我可以打印出来。
但是,如果用户确实在共享首选项中有一个名称,而if / else块在没有弹出输入窗口的情况下向下跳到其他位置,则完全相同的buildHighScores()函数可以正常工作。
我意识到在点击监听器中完成所有这些工作是愚蠢的,但我已经尝试了四处移动并以不同的顺序执行但没有成功。我也知道整个事情可以更好地构建,但我不确定为什么这不起作用。谢谢。
...初始化一切......
// click listener for input window button
btnSubmitName.addListener(new ClickListener() {
@Override
public void clicked(
com.badlogic.gdx.scenes.scene2d.InputEvent event, float x,
float y) {
super.clicked(event, x, y);
stage.addAction(Actions.sequence(Actions.fadeOut(1), Actions.run(new Runnable() {
@Override
public void run() {
try {
// check database if name is taken
if(netcode.IsNameTaken(txtVal) == false) {
prefs.putString("name", txtVal);
prefs.flush();
System.out.println("thnx " + txtVal + " m8");
/////////////////////////////////////////
// doesn't display table, only debug lines
/////////////////////////////////////////
buildHighScores();
inputOK = true;
}
else System.out.println("name is taken");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
})));
}
});
if(prefs.getString("name", null) == null)
{
System.out.println("ok...");
try {
netcode.setState(0);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Adding Input Window...");
stage.addActor(window);
}
else
{
System.out.println("Name not null...");
//////////////////////////
// displays table perfectly
//////////////////////////
buildHighScores();
}
... stage.act& stage.draw ...
public void buildHighScores() {
playerdata.setPlayerName(prefs.getString("name"));
System.out.println("hey " + playerdata.getPlayerName());
highScoreString = prefs.getString("highscore");
String decodedString = Base64Coder.decodeString(highScoreString);
System.out.println(decodedString);
playerdata.setHighScore(Integer.parseInt(decodedString));
try {
netcode.setState(1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// connect to server and update play
try {
netcode.serverConnect(playerdata);
} catch (ConnectException e) {
System.out.println("socket exception brah");
//gsm.setState(0);
e.printStackTrace();
//stage.addActor(errorWindow);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
// get high score list from server
HighScoreList = netcode.GetHighScores();
// not necessary
for(int i = 0 ; i < HighScoreList.size() ; i++) {
String tmp = new String(i+1 + " " + HighScoreList.get(i).getPlayerName() + " " + HighScoreList.get(i).getHighScore());
HighScoresList.add(tmp);
//System.out.println(tmp);
}
// build scoresTable with the high score list we got from server
scoresTable.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
for (int i = 0 ; i < HighScoresList.size() ; i++){
scoresTable.add(new Label(""+(i+1), labelStyle)).left();
scoresTable.add().width(Gdx.graphics.getWidth()/6);
scoresTable.add(new Label(HighScoreList.get(i).getPlayerName(), labelStyle)).left();
scoresTable.add().width(Gdx.graphics.getWidth()/6);
scoresTable.add(new Label(""+HighScoreList.get(i).getHighScore(), labelStyle)).left();
scoresTable.row();
//System.out.println(i);
}
scoresTable.debug();
// add scoresTable to a scrollpane
scrollpane = new ScrollPane(scoresTable);
// create table to display high scores
table.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
table.add(label);
table.row();
table.add(textfield).width(Gdx.graphics.getWidth()/2);
table.row();
table.add(scrollpane).fill().height(Gdx.graphics.getHeight()/2);
table.row();
table.add(button2);
table.debug();
stage.clear();
stage.addActor(table);
}