2014-03-04 43 views
0

我已经发布了这个question之前,但似乎我无处可去,即使我确实得到了很多人的帮助。所以我做了一个小实验。我创建了一个测试项目来测试LibGdx触摸处理。这touchTester项目以某种方式复制我的问题。附件(附件删除)请找到整个项目源代码(压缩文件)。以下代码中的upCounter应该只返回1,因为它只应该需要运行一次。libgdx演员touchHandling问题

// 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文件设置我的项目。

回答

1

您错过了这里最重要的部分。您添加new InputListener...的给定代码位于render()方法内。

这不是它应该如何。它的基本功能是在每一帧中为图片添加一个新的匿名InputListener。所有这些听众都会收到通知,他们都会在您的upcounter上加1。将代码移动到您的方法show(),它应该按预期工作。

+0

Thanks @noone。有道理。但是,如果我将这些代码放在show()方法中,在应用程序会话期间如何禁用/重新启用按钮/ InputListener? – Ziiiii

+0

'this.libgdxImg.cleaerListeners()',或者通过禁用整个阶段'Gdx.input.setInputProcessor(null)' – noone

+0

Thanks @noone,那么重新启用它呢? – Ziiiii