문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top