Java 如何在libgdx中向纹理打包器添加操作?
我试图在每个图像上创建eventListner(playIcon、PauseCon),但使用触地和触地都不起作用 这是我的密码:Java 如何在libgdx中向纹理打包器添加操作?,java,android,libgdx,Java,Android,Libgdx,我试图在每个图像上创建eventListner(playIcon、PauseCon),但使用触地和触地都不起作用 这是我的密码: TextureAtlas buttonsPlayPause = new TextureAtlas("uiii/uiii.pack"); skin.addRegions(buttonsPlayPause); TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle(); tex
TextureAtlas buttonsPlayPause = new TextureAtlas("uiii/uiii.pack");
skin.addRegions(buttonsPlayPause);
TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle();
textButtonStyle.font = font;
textButtonStyle.up = skin.getDrawable("pauseIcon");
textButtonStyle.checked = skin.getDrawable("playIcon");
TextButton button = new TextButton("", textButtonStyle);
button.setY(pauseY);
button.setX(Gdx.graphics.getWidth()/6);
button.setSize(150,150);
stage.addActor(button);
//pause/playAction
button.addListener(new ClickListener() {
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {resume();
}
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
pause();
return true;
}
});
现在发生的是按钮侦听器的行为是“istouched()”而不是“justtouched()”。当我点击按钮时,游戏暂停,但每当我移开手指时,没有暂停,游戏就会运行。如果我理解正确,您希望此按钮在播放和暂停之间切换,根据当前状态调用
pause()
或resume()
Button类已经有一个内置的内部InputListener,因此您只需要一个ChangeListener来对按钮按下做出反应:
//pause/playAction
button.addListener(new ChangeListener() {
@Override
public void changed (ChangeEvent event, Actor actor) {
if (button.isChecked())
resume();
else
play();
}
});
确保在首次声明时将其标记为final,以便侦听器可以引用它:
//...
final TextButton button = new TextButton("", textButtonStyle);
//...
您是否通过这个Gdx.input.setInputProcessor(stage)将stage设置为InputProcessor;从你的评论来看,你还不清楚你想要发生什么,以及它目前的行为有什么问题。@Tenfour04。现在发生的是按钮侦听器的行为是“istouched()”而不是“justtouched()”。当我点击按钮时,游戏暂停,但每当我移开手指,游戏就没有暂停runs@AbhishekAryan请阅读我的编辑,这是预期的,因为您在
toucup
方法中调用了resume()
。