Libgdx InputListener在TextButton中没有调用"exit()"方法

3

我希望在InputListener中使用exit()方法来判断光标是否在按钮内。

以下是libGDX文档中的解释。

public void exit(InputEvent event, float x, float y, int pointer, Actor toActor)

每当鼠标光标或手指触摸移出演员时调用。
但是,当我把光标放在按钮上,然后将其移动到按钮外时,该方法不会被调用。我通过 System.out.println("exited"); 进行测试,控制台没有任何输出。
编辑:
LibGDX 版本:最新稳定版夜间版本
InputListener 实现:
//This button class is a custom class to make button creation easier. This is the constructor.
public Button(Vector2 position, String packLocation, String text, Stage stage, BitmapFont font, Color color) {

//Removed buttonStyle creation etc. to shorten the code.
    button = new TextButton(text, buttonStyle);
    button.setPosition(position.x, position.y);
    stage.addActor(button);
    Gdx.input.setInputProcessor(stage);
    pressed = false;

    button.addListener(new ClickListener() {

        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            pressed = true;
            return true;
        }

        @Override
        public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
            pressed = false;
        }

        @Override
        public void exit(InputEvent event, float x, float y, int pointer, Actor toActor) {
            System.out.println("exited");
        }
    });
}

编辑:

悬停在按钮上也不会改变按钮的纹理,因为我已经设置了如下内容:

buttonStyle.over = skin.getDrawable("over");

但是点击会改变。


退出方法对我来说很有效。你使用的是哪个libgdx版本?你在哪里以及如何向按钮添加监听器?请添加相关代码。 - donfuxx
@donfuxx 已进行了编辑。注意: touchUp和touchDown是正常工作的。退出功能无法使用。 - Sierox
将您的监听器复制粘贴到我的一些按钮上,就像这样,它可以正常工作。您是在libgdx的桌面项目中测试吗?请注意,在Android中没有鼠标光标。 - donfuxx
@donfuxx 我在我的安卓设备和桌面上都没有收到“已退出”的消息。也许按钮初始化有问题? - Sierox
@donfuxx 另外,即使我已经为按钮设置了纹理,但当我的鼠标悬停在其上时,它的纹理也不会改变,因此我认为 enter() 和 exit() 存在一般性问题... - Sierox
1个回答

6

在搜索了几个小时后,我终于找到了缺失的部分。必须在渲染方法中调用 stage.act();。这不仅为当我们悬停在按钮上时给纹理更改提供了功能,还为InputListener中的enter/exit方法提供了功能。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接