Android 将手指拖开后仍调用ImageButton touch事件

Android 将手指拖开后仍调用ImageButton touch事件,android,button,libgdx,imagebutton,Android,Button,Libgdx,Imagebutton,我在LibGDX游戏中有一个ImageButton,其中有一个小错误可能会激怒用户。如果我按下按钮,但决定不点击它,我会将手指拖开。 但是,即使我的手指在拖走ImageButton后不再位于其顶部,仍会调用touchUp()方法 我怎样才能阻止补强赛的发生 我不知道是否以某种方式获得ImageButton的边界(这将如何进行),并查看润色位置是否对应,可能会起作用。我已经试着查找了,但是到目前为止我还没有找到任何东西,因为我的问题非常具体 以下是我初始化按钮的方式: retryButton =

我在LibGDX游戏中有一个ImageButton,其中有一个小错误可能会激怒用户。如果我按下按钮,但决定不点击它,我会将手指拖开。
但是,即使我的手指在拖走ImageButton后不再位于其顶部,仍会调用
touchUp()
方法

我怎样才能阻止补强赛的发生

我不知道是否以某种方式获得ImageButton的边界(这将如何进行),并查看润色位置是否对应,可能会起作用。我已经试着查找了,但是到目前为止我还没有找到任何东西,因为我的问题非常具体

以下是我初始化按钮的方式:

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();
            }
        }
    });

您应该使用
ClickListener
而不是
InputListener
,并且不要覆盖
touchUp
方法,而是覆盖
clicked
方法。

扩展grimrader22的答案,我还建议使用ClickListener来处理触摸事件,但是,即使您将手指拖到ImageButton之外,您仍然会遇到同样的问题,即在
触碰
事件中触发代码,这就是为什么
单击
方法应该可以正常工作的原因

但是,如果您想使用
touchUp
touchUp
方法,可以这样做:
touchUp
touchUp
中的x和y值表示图像按钮上触摸事件的局部坐标。因此,简单的解决方案是确保
修补
方法中事件的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();
        }
    }
@覆盖
公共无效修补(InputEvent事件、浮点x、浮点y、整数指针、整数按钮){
布尔inX=x>=0&&x=0&&y
谢谢。。。你们知道为什么我的问题被否决了吗?Button已经内置了一个ClickListener——添加一个ChangeListener比添加另一个ClickListener更有意义,以避免冗余的输入处理。不知道你的问题为什么被否决了——看起来不错。