gpt4 book ai didi

android - 将 ClickListener 添加到 TextButton LibGDX

转载 作者:行者123 更新时间:2023-11-29 17:43:56 24 4
gpt4 key购买 nike

我正在为 Android 开发一个应用程序,但我一直在为我的按钮创建监听器。我已经阅读了大量文章并进行了几个小时的测试,但它就是行不通。我确实喜欢这个例子:https://gist.github.com/mattdesl/5461944我什至在我的代码中没有错误,但我的按钮根本没有任何反应。

以下是我的尝试方式(或至少几次尝试中的一次):

...public MainMenuScreen(final Stapler gam) {
game = gam;

stage = new Stage();
table = new Table();
table.setFillParent(true);
stage.addActor(table);
Gdx.input.setInputProcessor(stage);

font = new BitmapFont(Gdx.files.internal("fonts/Blox.fnt"));
bodoque = new BitmapFont(Gdx.files.internal("fonts/bodoque.fnt"));

shapeRenderer = new ShapeRenderer();

// Add widgets to the table here.

TextureRegion upRegion = new TextureRegion(new Texture(
Gdx.files.internal("boxLila.png")));
TextureRegion downRegion = new TextureRegion(new Texture(
Gdx.files.internal("boxGruen.png")));
BitmapFont buttonFont = new BitmapFont(
Gdx.files.internal("fonts/Blox.fnt"), false);

TextButtonStyle style = new TextButtonStyle();
style.up = new TextureRegionDrawable(upRegion);
style.down = new TextureRegionDrawable(downRegion);
style.font = buttonFont;

play = new TextButton("Play", style);
table.add(play);
play.addListener(new ClickListener() {
public void clicked(InputEvent e, float x, float y) {
game.setScreen(game.gameScreen);
Gdx.app.log("Click", "performed"); // -> never happend
}
});

// add the button with a fixed width
table.add(play).width(500);
// then move down a row
table.row();

}

...

我解决了或至少找到了它不起作用的原因。我用自己的 InputProcessor 创建了一个 gamworld 实例,当我不创建它时,监听器工作..但现在我必须弄清楚如何同时使用这两者..

最佳答案

您可以为此添加一个 ChangeListener。像这样的东西:

    play.addListener(new ChangeListener() {
public void changed (ChangeEvent event, Actor actor) {
game.setScreen(game.gameScreen);
}
});

这是听按钮点击的方式,就像在 libgdx scene2 wiki 中解释的那样.

关于android - 将 ClickListener 添加到 TextButton LibGDX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27714970/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com