我已经发布了此question以前,但似乎我毫无进展,尽管我确实得到了很多人的帮助。因此我做了一个小实验。我创建了一个测试项目只是为了测试 LibGdx 触摸处理。这个 touchTester 项目以某种方式复制了我的问题。附件(附件已删除)请找到整个项目源代码(压缩文件)。以下代码中的 upCounter 应该只返回 1,因为它只需要运行一次。
// upCounter is = 0;
this.libgdxImg.addListener(new InputListener() {
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
return true;
}
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
upCounter++;
touchtester.doLog("upCounter = " + upCounter);
}
});
但是,当我运行它时,它给出了这个结果
catland: touchTester: render
catland: touchTester: render
catland: touchTester: upCounter = 1
catland: touchTester: upCounter = 2
...
catland: touchTester: upCounter = 94
catland: touchTester: upCounter = 95
catland: touchTester: render
catland: touchTester: render
我可以请人帮忙测试一下吗?我完全不知道问题从何而来。我使用 gdx-setup-ui.jar 文件设置我的项目。
最佳答案
你错过了这里最重要的部分。添加 new InputListener...
的给定代码位于 render()
方法内。
事情不应该是这样的。它的基本作用是在每个帧中向图像添加一个新的匿名 InputListener
。所有这些听众都会收到通知,并且他们都会为您的 upcounter
加 1。将代码移至您的 show()
方法,它应该按您的预期工作。
关于java - libgdx actor touch处理问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22161809/