在LibGDX中使用TextField [英] Using TextField with LibGDX

查看:139
本文介绍了在LibGDX中使用TextField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用LibGDX开发Android游戏,并希望实现两个TextField来登录服务器.

I am working on an Android game with LibGDX and would like to implement two TextFields to login to a server.

据我所知,我需要使用

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可以正确渲染,但是我无法对其进行任何操作,键盘也没有显示,等等.有人可以解释一下如何在我的项目中使用它们吗?

TextFields 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 ?

推荐答案

您知道需要使用StageTextField来满足您的要求,您创建了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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆