我想使用 InputListener 中的 exit() 方法来查看光标是否在按钮内。
这是 libGDX 文档中的解释。
public void exit(InputEvent event, float x, float y, int pointer, Actor toActor)
Called any time the mouse cursor or a finger touch is moved out of an actor.
但是当我将光标放在按钮上然后将其移到按钮外时,不会调用该方法。我正在通过 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");
但是点击会。
最佳答案
经过几个小时的搜索,我终于找到了丢失的东西。 stage.act();
必须在 render 方法中调用。当我们将鼠标悬停在按钮上时,这既为纹理更改提供了功能,也为 InputListener 中的进入/退出方法提供了功能。
关于java - 未使用 TextButton 调用 Libgdx InputListener "exit()",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24788443/