AndroidStudio libgdx ChangeListener无法正常工作 [英] AndroidStudio libgdx ChangeListener not working

查看:98
本文介绍了AndroidStudio libgdx ChangeListener无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android Studio-libgdx制作2D游戏.我有一个Main创建MenuState,在我制作的render方法中

Android Studio-libgdx making a 2d game. I have a Main which creates the MenuState, in the render method I made this

    @Override
    public void render(float delta) {
    Gdx.gl.glClearColor(0, 0, 0.2f, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    camera.update();
    myGame.batch.setProjectionMatrix(camera.combined);
    batch = new SpriteBatch();
    skin = new Skin(Gdx.files.internal("data/uiskin.json"));
    stage = new Stage();

    final TextButton button = new TextButton("Click me", skin, "default");

    button.setWidth(400f);
    button.setHeight(100f);
    button.setPosition(Gdx.graphics.getWidth() / 2 - 100f, Gdx.graphics.getHeight() / 2 - 10f);

    button.addListener(new ChangeListener() {
        @Override
        public void changed(ChangeEvent event, Actor actor) {
            Gdx.gl.glClearColor(1, 0, 0, 1);
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        }
    });
    stage.addActor(button);

    Gdx.input.setInputProcessor(stage);

    myGame.batch.begin();
    stage.draw();
    myGame.batch.end();
}

我检查了一些有关如何制作菜单的教程,这几乎就是我在这里要测试的内容.但是,当我运行该应用程序时,该按钮出现,如果单击它,则什么也没有发生.我尝试使用ClickListener进行测试,但仍然无法正常工作.有任何想法吗?还是我做错了,还是应该用另一种方法?我是libgdx的新手.提前致谢.

I checked some tutorials on how to make a menu and that's pretty much what I'm testing in here. But when I run the app the button appears and nothing happens if I click it. I've tried testing with the ClickListener but still doesn't work. Any ideas? Or am I doing it wrong or it should be in another method? I'm new with libgdx. Thanks in advance.

推荐答案

您要在render方法中创建舞台,外观和批处理的新实例吗?将它们从渲染方法中移开;)

You are creating new instances of the stage, skin and batch in render method? Move them away from your render method ;)

渲染应该看起来像这样:

Render should look more like this:

    @Override
        public void render(float delta) {
        Gdx.gl.glClearColor(0, 0, 0.2f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        camera.update();
        myGame.batch.setProjectionMatrix(camera.combined);

        myGame.batch.begin();
        stage.draw();
        myGame.batch.end();
}

在构造函数中:

    batch = new SpriteBatch();
    skin = new Skin(Gdx.files.internal("data/uiskin.json"));
    stage = new Stage();

    final TextButton button = new TextButton("Click me", skin, "default");

    button.setWidth(400f);
    button.setHeight(100f);
    button.setPosition(Gdx.graphics.getWidth() / 2 - 100f, Gdx.graphics.getHeight() / 2 - 10f);

    button.addListener(new ChangeListener() {
        @Override
        public void changed(ChangeEvent event, Actor actor) {
            Gdx.gl.glClearColor(1, 0, 0, 1);
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        }
    });
    stage.addActor(button);

    Gdx.input.setInputProcessor(stage);

这篇关于AndroidStudio libgdx ChangeListener无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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