如何用两个指针检测触摸/释放两个对象

时间:2016-03-29 13:12:40

标签: android libgdx

我试图用两根手指同时触摸两个矩形。我该如何实现呢?

1 个答案:

答案 0 :(得分:1)

没关系,我找到了解决这个问题的方法:

public void detectTwoRectangleTouching() {

        if(Gdx.input.isTouched(0)){
             int x = Gdx.input.getX(0);
             int y = Gdx.input.getY(0);

             if(rectangle1.contain(x,y))
                 isRectangle1Touched = true;
             else
                 isRectangle1Touched = false;


             if(rectangle2.contain(x,y))
                 isRectangle2Touched = true;
             else
                 isRectangle2Touched = false;

        }else {
             isRectangle1Touched = false;
             isRectangle2Touched = false;

        }

        if(Gdx.input.isTouched(1)){
            int x = Gdx.input.getX(1);
            int y = Gdx.input.getY(1);

            if(rectangle1.contain(x,y))
                isRectangle1Touched = true;

            if(rectangle2.contain(x,y))
                isRectangle2Touched = true;

        }

        if(isRectangle1Touched && isRectangle2Touched) {
            // the two rectangle are pressed !!!
        }

        if(!isRectangle1Touched && !isRectangle2Touched) {
            // the two rectangle are released !!!
        }
    }