libgdx演员touchHandling问题 [英] libgdx actor touchHandling issue
问题描述
I have posted this question before but it seems that I am heading nowhere even though I did receive help from many people. Therefore I have done a small experiment. I have created a test project just to test out the LibGdx touch handling. This touchTester project somehow replicates my problem. Attached (attachment removed) please find the entire project source code (zipped file). The upCounter in following code should only return 1 since it should only be needed to run once.
// 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);
}
});
然而,当我运行它,它给了我这样的结果。
However, when I run it, it give me this result
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-设置-ui.jar文件我的项目。
May I ask someone's help to test it out? I have absolutely no idea where the problem came from. I setup my project using the gdx-setup-ui.jar file.
推荐答案
您错过了这里最重要的部分。给定的code其中您添加新InputListener ...
里面的渲染()
方法。
You missed the most important part here. The given code where you add your new InputListener...
is inside your render()
method.
这不是应该的。它通常做的是在每一帧中添加一个新的,匿名的 InputListener
你的形象。所有这些听众将得到通知,并都加1到你的加计数器
。移动code到你的显示()
方法和你预期它应该工作。
That's not how it should be. What it basically does is adding a new, anonymous InputListener
to your image in every single frame. All those listeners will get notified and they all add 1 to your upcounter
. Move the code to your show()
method and it should work as you expected.
这篇关于libgdx演员touchHandling问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!