我正在制作一个应用程序,我想添加ImageButton按钮。我不明白为什么我的addListener()不起作用,即使按钮
- 初始化
- 添加到场景中
- 正确渲染
在MainGameScreen类的构造函数中,实例化setInputProcessor并将其设置为当前场景:
_viewport = new ScreenViewport();
_stage = new Stage();
iniatilizeButtons();
Gdx.input.setInputProcessor(_stage);
iniatilizeButtons ()函数描述了按钮的样式,按钮立即添加到场景中并定义了addListener():
buttonTextureAtlas = new TextureAtlas("ui/joystick/joystick.atlas");
skin = new Skin();
skin.addRegions(buttonTextureAtlas);
buttonRightStyle = new ImageButton.ImageButtonStyle();
buttonRightStyle.down = skin.getDrawable("touchBackground");
buttonRightStyle.up = skin.getDrawable("touchKnob");
buttonRightStyle.checked = skin.getDrawable("touchKnob");
buttonRight = new ImageButton(buttonRightStyle);
buttonRight.setPosition(20, 20);
buttonRight.setTransform(true);
buttonRight.setScale(0.001f*Gdx.graphics.getHeight());
_stage.addActor(buttonRight);
buttonRight.addListener(new ClickListener(){
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
super.touchUp(event, x, y, pointer, button);
Gdx.app.log(TAG, "Button Pressed");
hide();
}
});
当调用render()我这样做:
_stage.act(delta);
_stage.draw();
稍微清楚一点,按下按钮并释放它不会输出我在 touchUp() 中指定的“按下按钮”。我已经添加了 touchDown() 和 clicked(),但它们在单击时也不显示任何内容。
尝试
Gdx.input.setInputProcessor(_stage);不在构造函数中调用,而是在方法中调用create()