LibGDX - 使用InputProcessor进行多点触控处理

时间:2015-01-24 21:31:41

标签: java android libgdx multi-touch

我试图在我的Android游戏中设置适当的多点触控支持一段时间。对于输入处理,我使用我的InputHandler(实现InputProcessor)。我使用这样的东西

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    Vector3 touchPos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
    screen.renderer.cam.unproject(touchPos);
    if (left.isTouchDown((int) touchPos.x, (int) touchPos.y)) {
        player.setMovingLeft(true);
        player.leftPressed();
    }

和这个

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
    Vector3 touchPos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
    screen.renderer.cam.unproject(touchPos);
    if (left.isTouchUp((int) touchPos.x, (int) touchPos.y)) {
        player.setMovingLeft(false);
        player.leftReleased();
    }

我已经设置了3个这样的按钮,但我不知道它为什么不起作用。在使用InputProcessor时,我发现的所有答案都没有用。

我知道它与触摸指针和迭代输入有关,但不幸的是,这就是我的知识结束的地方。

代码"触及"和"发布后#34;我确实工作,我用键盘输入的方式相同。唯一的问题是多点触控。

3 个答案:

答案 0 :(得分:2)

我曾经遇到过类似的问题,试图在屏幕上设置3个按钮。这些按钮可以单独按下(然后没有问题,你总是只使用一个手指和一个指针 - 因此可以忽略指针ID),但是如果按下任何两个按钮,它们也应该正确反应,或者玩家按下所有按钮立刻。 您必须使用由InputProcessor提供的指针ID。指针ID是0到9范围内的int(10个手指)。 我这样做了:

首先为三个指针声明了三个变量:

int forwardPointer, leftPointer, rightPointer;

然后在TouchDown方法中检查,哪个手指按下哪个按钮(是第一次,第二次还是第三次按?),并将此信息存储在我的变量中以备将来使用。

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    if (game.forwardButton.contains(screenX, screenY)) {
        game.forwardButton.isPressed=true;
        forwardPointer=pointer; 
    }
    if (game.leftButton.contains(screenX, screenY)) {
        game.leftButton.isPressed=true;
        leftPointer=pointer;
    }
    if (game.rightButton.contains(screenX, screenY)) {
        game.rightButton.isPressed=true;
        rightPointer=pointer;
    }}

然后,在TouchUp方法中,我检查了哪个手指被抬起(通过比较TouchUp方法中的指针ID和存储的指针变量值),并根据此,我“重新”了正确的按钮。

    @Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
    if (pointer==forwardPointer) {
        game.forwardButton.isPressed=false;
    }
    if (pointer==leftPointer) {
        game.leftButton.isPressed=false;
    }
    if (pointer==rightPointer) {
        game.rightButton.isPressed=false;
    }}

这对我有用。无需使用Gdx.input.getX(),Gdx.input.getY(),as 这些值已经由touchDown和touchUp方法(screenX和screenY)给出。

答案 1 :(得分:1)

您正在将libGDX输入轮询接口与libGDX输入事件API混合。具体来说,这些电话:

 Gdx.input.getX(), Gdx.input.getY()

相当于:

 Gdx.input.getX(0), Gdx.input.getY(0)

the getX documentation。因此,您始终会读取第0个触摸索引的x / y坐标,而不管哪个触摸处于活动状态。

事件API正在传递in the x, y and pointerID to the callback,因此您只需执行以下操作:

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
Vector3 touchPos = new Vector3(screenX, screenY, 0);
screen.renderer.cam.unproject(touchPos);
if (left.isTouchDown((int) touchPos.x, (int) touchPos.y)) {
    player.setMovingLeft(true);
    player.leftPressed();
}

您应该使用“指针”参数来区分多点触控时的手指。

答案 2 :(得分:1)

由于在事件方法中传递的指针值是触摸手指的唯一ID,因此您可以使用散列图来存储和操作具有该唯一键的所有触摸。

创建一个hashmap。

public static HashMap<Integer, Vector2> touchlist;
public Constructor(){
    touchlist=new HashMap<>();
}

如果没有触摸,则添加为新的。

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    if(!touchlist.containsKey(pointer))touchlist.put(pointer, new Vector2(screenX, screenY));
    return false;
}

如果手指向上移除。

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

如果拖动手指,则更新相应的触摸事件

@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
    Vector2 touch=touchlist.get(pointer);
    if(touch!=null)touch.set(screenX, screenY);
    return false;
}

键是指针,值是一个存储触摸坐标的矢量。 并在任何你想要的地方使用它们只是迭代地图

for(Integer key:touchlist.keySet()){
    Vector2 touchpos=touchlist.get(key);
    //do your manipulation here
    if(isvalid(touchpos)) //use if valid skip if not etc..
}