libgdx的touchUp事件

4
我正在使用舞台上的演员作为按钮。我可以很好地检测到触摸/释放事件发生在演员上,但是当用户点击演员并然后拖动手指离开演员时,touchUp事件从未触发。我尝试使用exit事件,但它从未触发。在我的程序中,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
你在使用哪个版本的libgdx?最新版似乎没有Stage.addListener()。也许这是一个错误,在比你所使用的版本更新的版本中已经修复了。 - kichik
我正在使用2012年10月20日的每夜版构建,libgdx.txt显示为0.9.3,那我接下来会尝试0.9.6。 - Skepte Cist
啊,我不太熟悉最前沿的技术。如果我要猜的话,我会说实现 touchDragged() 可能有帮助。 - kichik
我也试过了,但没有成功,它从未触发。 - Skepte Cist
2个回答

3
如果您更改
stage.addListener(new InputListener() {});

to

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

它将识别TouchUp调用。同时还能处理TouchDown和Exit调用。


为什么?这是一个错误吗?它会被修复吗? - Winter

1
我遇到了同样的问题。我通过在我的GameScreen类中创建一个boolean isDown变量来解决它。每当在我的背景图片上发生touchDown时,我将isDown变量设置为true,当touchUp发生时,isDown = false。这样touchUp就会一直发生。然后,在我的GameScreen的render方法中,我检查isDown是否为true,如果是,我检查触摸是否与我的actor相交:
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;
}

这是我找到的唯一解决方法。虽然不太美观,但对我有用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接