libgdx actor touchHandling issue

十年热恋 提交于 2019-12-12 04:44:05

问题


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

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.


回答1:


You missed the most important part here. The given code where you add your new InputListener... is inside your render() method.

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.



来源:https://stackoverflow.com/questions/22161809/libgdx-actor-touchhandling-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!