【发布时间】:2014-07-16 18:50:10
【问题描述】:
我想在 InputListener 中使用 exit() 方法来查看光标是否在按钮内。
这是 libGDX 文档中的解释。
public void exit(InputEvent event, float x, float y, int pointer, Actor toActor)
在鼠标光标或手指触摸移出演员时调用。
但是当我将光标放在按钮上然后将其移到按钮外时,不会调用该方法。我正在通过 System.out.println("exited"); 对其进行测试,但在控制台中什么也没有。
编辑:
LibGDX 版本: 最新稳定的 Nightlies
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 进行了编辑。 注意: touchUp 和 touchDown 正在工作。退出不是。
-
将你的听众复制粘贴到我的一些按钮上,它就像这样工作得很好。你在 libgdx 的桌面项目中测试这个吗?请注意,Android 中没有鼠标光标。
-
@donfuxx 我的 Android 设备和桌面都没有收到“退出”消息。可能是按钮初始化错误?
-
@donfuxx ALSO,当我的鼠标悬停在按钮上时,按钮的纹理不会改变,即使我确实为它设置了纹理,所以我认为 enter() 和 exit() 存在一般问题...
标签: java android input libgdx listener