我的LibGDX游戏中有一个ImageButton,其中有一个可能会惹恼用户的小错误。如果我按下按钮,但决定不想点击它,我会把手指拉开
但是,即使我的手指在拖动之后不再位于ImageButton的顶部,仍然会调用touchUp()
方法。
如何阻止touchUp事件发生?
我不知道是否以某种方式获得该ImageButton的界限(如何去),并且看看touchUp位置是否对应,可能会有效。我已经尝试过了,但到目前为止我找不到任何东西,因为我的问题非常具体。
这就是我初始化按钮的方式:
retryButton = new ImageButton(getDrawable(new Texture("RetryButtonUp.jpg")), getDrawable(new Texture("RetryButtonDown.jpg")));
retryButton.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
return true;
}
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
if(touchable) {
game.setScreen(new PlayScreen(game, difficulty));
dispose();
}
}
});
答案 0 :(得分:1)
您应该使用ClickListener
而不是InputListener
,而不是覆盖touchUp
方法,而是覆盖clicked
方法。
答案 1 :(得分:0)
延长grimrader22的答案,我还建议使用ClickListener来处理触摸事件,但即使你把手指拖到外面,你仍会遇到touchUp
事件中触发代码的相同问题ImageButton,这就是clicked
方法应该正常工作的原因。
但是,如果您想使用touchUp
和touchDown
方法,请执行以下操作:touchUp
和touchDown
中的x和y值代表本地坐标ImageButton上的触摸事件。因此,简单的解决方案是确保touchUp
方法中事件的x和y都在ImageButton的本地坐标内...
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
boolean inX = x >= 0 && x < getWidth();
boolean inY = y >= 0 && y < getHeight();
if(inX && inY && touchable) {
game.setScreen(new PlayScreen(game, difficulty));
dispose();
}
}