检测手指何时离开屏幕并更新指针

时间:2015-01-29 08:26:46

标签: java android libgdx multi-touch

我正在制作一个游戏,我加速在屏幕上握住两根手指,一只手指放在左半边,一根手指放在显示屏的右半边。 如果你松开手指并放下另一个手指,无论它是什么,我都必须根据手指的位置弯曲车辆。如果你位于右半边(Gdx.grapchis.getWidth / 2),那么我弯向右边......左边。

输入处理器的一部分:

        @Override
        public boolean touchDown(int screenX, int screenY, int pointer, int button) {
            if(pointer < 2)
            {
                CoordinateTouch tmp = new CoordinateTouch();
                tmp.x = screenX;
                tmp.y = screenY;
                coordinates.add(tmp);
            }
            return false;
        }

        @Override
        public boolean touchUp(int screenX, int screenY, int pointer, int button) {
            coordinates.clear();
            return false;
        }

我的坐标数组:

public class CoordinateTouch{
    float x;
    float y;
}

List<CoordinateTouch> coordinates;

控制渲染方法中的指针(组是我的纹理):

if(coordinates.size() > 1)
    {
        group.addAction(parallel(moveTo(realDest.x, realDest.y, (float) 15)));           
    }
    else
    {
        group.addAction(delay((float)1.5));
        group.clearActions();
        if(Gdx.input.isButtonPressed(0)) {
            if (Gdx.input.getX() < Gdx.graphics.getWidth() / 2) {
                group.addAction(parallel(rotateBy(velocityRotazionShip, (float) 0.03)));
            } else {
                group.addAction(parallel(rotateBy(-velocityRotazionShip, (float) 0.03)));
            }
        }
    }

我的问题是,如果我让一根手指检测到它,并且到目前为止这么好,但是如果我只是将手指拉开,我不会更新指针并且没有产生代码组.addAction。

我也试过isButtonPressed和isKeypressed,isTouched(index),但结果是一样的。

对不起英语,我希望已经清楚了。

1 个答案:

答案 0 :(得分:1)

如果我理解你,你有3个案例:

  1. 两个不同的侧面都被按下 - &gt;移动
  2. 按下左侧显示侧 - &gt;向左倾斜
  3. 按下右侧显示侧 - &gt;向右倾斜
  4. 如果这个假设是正确的,你只需要boolean s:

    boolean touchLeft, touchRight
    

    touchDown内你可以做类似的事情:

     public boolean touchDown(int screenX, int screenY, int pointer, int button) {
     if (screenX < Gdx.graphics.getWidth()/2)
         touchLeft = true;
     else
         touchRight = true;
     }
    

    touchUp

    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        if (screenX < Gdx.graphics.getWidth()/2)
            touchLeft = false;
        else
            touchRight = false;
    }
    

    现在在render内你可以说:

    if (touchLeft && touchRight)
        // move
    else if (touchLeft)
        // lean left
    else if (touchRight)
        // leanRight
    else
        // do nothing or something else
    

    如果您想支持多个手指/侧面,可以将boolean更改为int s,并指定手指/侧面的数量。在touchDown递增它,touchUp递减它和渲染询问,如果它是&gt; 0

相关问题