libgdx touchUp事件

时间:2012-11-02 20:41:27

标签: java events libgdx actor stage

我正在使用舞台上的演员作为按钮。我可以检测到touchDown / touchUp事件何时发生在演员身上,但是当用户点击演员然后继续将他们的手指拖离演员时,touchUp事件永远不会触发。我尝试使用退出事件,但它永远不会触发。在我的程序中,touchUp / touchDown事件确定移动和按钮颜色,这取决于按钮是否被按下。所以我'留下一个永久“按下”的按钮,直到它再次点击/向上。

我正在使用的代码示例:

stage.addListener(new InputListener() {

    public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
        Actor actor = stage.hit(x, y, true);
        if (actor != null){
            System.out.println("touchDown: " + actor.getName().toString()); 
        }
        return true;
    }

    public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
        Actor actor = stage.hit(x, y, true);
        if (actor != null){
                System.out.println("touchUp: " + actor.getName().toString());           
                }
        }

    public void exit(InputEvent event, float x, float y, int pointer, Actor toActor){
        System.out.println("exit");
    }
});

2 个答案:

答案 0 :(得分:3)

如果你改变了

stage.addListener(new InputListener() {});

stage.addListener(new ClickListener() {});

它会识别TouchUp通话。它仍然可以处理TouchDown和Exit调用。

答案 1 :(得分:1)

我遇到了同样的问题。我通过创建boolean isDown变量作为我的GameScreen类的字段来修复它。每当touchDown出现在我的背景图像上时,我将isDown变量设为true,并且当touchUp发生时 - isDown = false。这样就会发生touchUp。然后仍然在我的GameScreen渲染方法中,我检查isDown是否为真,如果是,我检查触摸是否与我的演员相交:

if (isDown) {
   if (pointIntersection(myActor, Gdx.input.getX(), Gdx.input.getY())) {
                // do something
   }
} else {
  // reverse the effect of what you did when isDown was true
}

其中pointIntersection方法是:

public static boolean pointIntersection(Image img, float x, float y) {
    y = Gdx.graphics.getHeight() - y;
    if (img.x <= x && img.y <= y && img.x + img.width >= x && img.y + img.height >= y)
        return true;

    return false;
}

这是我发现的唯一解决方法。不过,它不是很漂亮,但适合我。