在LibGDX中使用TextField [英] Using TextField with LibGDX
问题描述
我正在使用LibGDX开发Android游戏,并希望实现两个TextField
来登录服务器.
I am working on an Android game with LibGDX and would like to implement two TextField
s to login to a server.
据我所知,我需要使用 Actor
像这样:
As far as I know I need to use Stage
to add a TextField
as an Actor
like this:
this.stage = new Stage();
TextField txtf = new TextField("", mSkin);
//...
stage.addActor(txtf);
// And then set the stage as InputProcessor
Gdx.input.setInputProcessor(this.stage);
但是我已经有一个实现InputProcessor
的自定义InputHandler
.我真的不知道如何用InputHandler
处理TextField
.
But I already have a custom InputHandler
which implements InputProcessor
. I don't really know how I can handle a TextField
with my InputHandler
.
我已经在我的InputHandler
中创建了我的TextField
,如下所示:
I have created my TextField
in my InputHandler
like this:
public class InputHandler implements InputProcessor {
public InputHandler(float ratioWidth, float ratioHeight, GameWorld world) {
this.world = world;
this.player = world.getPlayer();
this.ratioWidth = ratioWidth;
this.ratioHeight = ratioHeight;
//...
this.usernameTextField = new TextField("", AssetLoader.defaultSkin);
this.usernameTextField.setPosition(24,73);
this.usernameTextField.setSize(88, 14);
this.passwordTextField = new TextField("", AssetLoader.defaultSkin);
this.passwordTextField.setPosition(24, 102);
this.passwordTextField.setSize(88, 14);
this.actors.add(this.usernameTextField);
this.actors.add(this.passwordTextField);
}
然后我的InputHandler
中有一个方法可以为GameRenderer
获取TextField
:
And then I have a method in my InputHandler
that can get the TextField
for the GameRenderer
:
public ArrayList<Actor> getActors() { return this.actors; }
这是我的GameRenderer
:
public class GameRenderer {
public GameRenderer(GameWorld world) {
this.world = world;
//...
}
//...
private void drawLogUI() {
for(Actor a : ((InputHandler) Gdx.input.getInputProcessor()).getActors()){
a.draw(batcher, 1);
}
//...
}
public void render(float delta) {
// Initialisation
Gdx.gl.glClearColor(32/255.0f, 72/255.0f, 132/255.0f, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batcher.begin();
batcher.enableBlending();
//...
drawLogUI();
//...
batcher.end();
}
//...
}
TextField
可以正确渲染,但是我无法对其进行任何操作,键盘也没有显示,等等.有人可以解释一下如何在我的项目中使用它们吗?
TextField
s are rendering correctly but I can't do anything with them, keyboard doesn't show up, etc. Can someone explain how I can use them in my project ?
推荐答案
您知道需要使用Stage
和TextField
来满足您的要求,您创建了TextField
而不是添加到舞台上,所以您没有使用scene2d(舞台,演员,文本字段..)
You know that you need to use Stage
and TextField
for your requirement, you created TextField
but not adding to stage so you're not using scene2d(stage, actor, textfield..)
您关注的教程没有舞台,因此他使用InputProcessor
来满足要求.
Tutorial that you following, don't have stage so he is using InputProcessor
for his requirement.
TextField usernameTextField = new TextField("", AssetLoader.defaultSkin);
usernameTextField.setPosition(24,73);
usernameTextField.setSize(88, 14);
stage.add(usernameTextField); // <-- Actor now on stage
Gdx.input.setInputProcessor(stage);
内部render()
方法调用
stage.draw();
stage.act();
这篇关于在LibGDX中使用TextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!