LibGDX - 多点触控操作使用InputProcessor [英] LibGDX - Multitouch handling using InputProcessor

查看:252
本文介绍了LibGDX - 多点触控操作使用InputProcessor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立在我一段时间的Andr​​oid游戏适当的多点触摸的支持。对于输入处理我用我的InputHandler(实施InputProcessor)。我用这样的事情

I am trying to set up proper multitouch support in my android game for some time. For input handling I use my InputHandler(implementing InputProcessor). I use something like this

    @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的时候都不是很有益的。

I have set up 3 buttons like that, but I have no idea why it doesn't work. All the answers I've found aren't very helpful when using InputProcessor.

我知道它是与触摸指针和迭代输入,但是这就是我的知识结束,很遗憾。

I know that it has something to do with touch pointers and iterating input, but that's where my knowledge ends, unfortunately.

在code时感动和释放时工作是肯定的,我使用它与键盘输入的方式相同。唯一的问题是多点触控。

The code "when touched" and "when released" is working for sure, I'm using it the same way with keyboard input. The only problem is multitouch.

推荐答案

我encoutered类似的问题一次,试图建立3个按键在屏幕上。这些按钮可以分别pssed $ P $(当时是没有问题的,你总是只用一个手指和一个指针 - 因此指针ID可以忽略不计),但他们也应该作出反应适当,如果他们中的任何两个是pressed,或播放器presses的一次。
你必须使用指针ID,由InputProcessor给出。指针ID是从0到9的范围(10指)一个int。
我做了这种方式:

I encoutered similar problem once,trying to set up 3 buttons on screen. Those buttons can be pressed separately (then there is no problem, you always use just one finger and one pointer - therefore pointer ID can be ignored), but they should also react properly, if any two of them are pressed, or player presses all at once. You have to make use of pointer ID, given by InputProcessor. Pointer ID is an int from 0 to 9 range (for 10 fingers). I did it that way:

首先声明三个变量三分:

first declared three variables for three pointers:

int forwardPointer, leftPointer, rightPointer;

然后TouchDown的方法我检查,哪个手指pressed哪个按钮(这是第一,第二或第三preSS),在我以备将来使用变量存储这些信息。

then in TouchDown method i checked, which finger pressed which button (was it first, second or third press?), and stored this information in my variables for future use.

@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;
    }}

,然后在润色方法我检查,其中手指抬起(通过比较与存储指针变量值润色方法指针ID)根据,为此,我relased适当的按钮。

and then, in TouchUp method i checked, which finger was lifted up (by comparing pointer ID from TouchUp method with stored pointer variable value), and according to this, i "relased" proper button.

    @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(),如
这些值是通过达阵和润色法(screenX和screenY)已经给了。

This worked for me. No need to use Gdx.input.getX(), Gdx.input.getY(), as those values are already given by touchDown and touchUp method (screenX and screenY).

这篇关于LibGDX - 多点触控操作使用InputProcessor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆