我在我的应用程序中创建了一个显示文本字段和按钮的屏幕但由于某种原因我无法点击它们。你知道可能是什么原因吗?那是我的代码(我跳过了变量声明):
public class SentenceScreen implements Screen {
public SentenceScreen(Game g) {
game = g;
}
@Override
public void render(float delta) {
// TODO Auto-generated method stub
Gdx.gl.glClearColor(0,0,0,0);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage = new Stage();
Skin skin = new Skin(Gdx.files.internal("uiskin.json"));
btnLogin = new TextButton("Click me", skin);
btnLogin.setPosition(300, 50);
btnLogin.setSize(100, 60);
stage.addActor(btnLogin);
textField = new TextField("", skin);
textField.setPosition(100, 50);
textField.setSize(190, 60);
stage.addActor(textField);
stage.act(delta);
stage.draw();
Gdx.input.setInputProcessor(stage);
}
}
答案 0 :(得分:2)
stage = new Stage();
Skin skin = new Skin(Gdx.files.internal("uiskin.json"));
btnLogin = new TextButton("Click me", skin);
btnLogin.setPosition(300, 50);
btnLogin.setSize(100, 60);
stage.addActor(btnLogin);
textField = new TextField("", skin);
textField.setPosition(100, 50);
textField.setSize(190, 60);
stage.addActor(textField);
Gdx.input.setInputProcessor(stage);
所有这些都不应该在你的render()方法中。相反,将实例化放在show()方法或consctructor中。它还将大大减少滞后。
为什么按钮不起作用:每帧都实例化一个新的Stage,并为其分配InputProcessor。实际代码中没有时间处理实际输入。
以下是您的课程应该是什么样的:
public class SentenceScreen implements Screen {
public SentenceScreen(Game g) {
game = g;
}
@Override
public void render(float delta) {
// TODO Auto-generated method stub
Gdx.gl.glClearColor(0,0,0,0);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage.act(delta);
stage.draw();
}
@Override
public void show() {
stage = new Stage();
Skin skin = new Skin(Gdx.files.internal("uiskin.json"));
btnLogin = new TextButton("Click me", skin);
btnLogin.setPosition(300, 50);
btnLogin.setSize(100, 60);
stage.addActor(btnLogin);
textField = new TextField("", skin);
textField.setPosition(100, 50);
textField.setSize(190, 60);
stage.addActor(textField);
Gdx.input.setInputProcessor(stage);
}
}