Using TextField with LibGDX

风格不统一 提交于 2019-12-11 07:33:16

问题


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);

But I already have a custom InputHandler which implements InputProcessor. I don't really know how I can handle a TextField with my InputHandler.

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);
    }

And then I have a method in my InputHandler that can get the TextField for the GameRenderer:

public ArrayList<Actor> getActors() { return this.actors; }

And this is my 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();
    }

    //...

}

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 ?


回答1:


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..)

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);

Inside render() method call

stage.draw();
stage.act();


来源:https://stackoverflow.com/questions/45014420/using-textfield-with-libgdx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!