Вопрос

I've completed my pc version and testing of my libgdx game, and on pc it seemed fine, but when I ran it on two phones with android (Samsung Galaxy S3 mini and Samsung Galaxy S2) I ran into some problems, which I didn't expect to happen. Basicly, on my 1v1 player version, every player has its own touch controls left and right. The player has a constant speed (lenght of the speed vector is constant) but whenever a player touches any of the controls, the direction of the speed vector changes. My code looks like this:

private void handleInput(float delta) {
    if (Gdx.input.isTouched()) {
        for (int i = 0; i < 20; i++) {

            if (Gdx.input.isTouched(i)) {

                touchedX = Gdx.input.getX(i);
                touchedY = Gdx.input.getY(i);
                // PLAYER 1
                if (touchedX <= dB) {
                    // RED RIGHT
                    if (touchedY <= h / 2 + h / playerSpacing + dB
                            && touchedY >= h / 2 + h / playerSpacing) {
                        turnLeft(players[0], delta);
                    }
                    // RED LEFT
                    else if (touchedY <= h / 2 - h / playerSpacing
                            && touchedY >= h / 2 - h / playerSpacing - dB) {
                        turnRight(players[0], delta);
                    }

                }
                //PLAYER 2
                else if (touchedX >= w - dB) {
                    // BLUE RIGHT 
                    if (touchedY <= h / 2 + h / playerSpacing + dB
                            && touchedY >= h / 2 + h / playerSpacing)  {
                        turnRight(players[1], delta);
                    }
                    // BLUE LEFT
                    else if (touchedY <= h / 2 - h / playerSpacing
                            && touchedY >= h / 2 - h / playerSpacing - dB) {
                        turnLeft(players[1], delta);
                    }
                }

            }
        }
    }

It is just not working well on the actual android device, the touches aren't fluently recognized and total control of players is compromised. Any ideas why it is happening? LibGdx has bad multitouch support or I did something wrong?

Это было полезно?

Решение

The problem is that you have not unprojected the touch points.

you must unproject the touch points as

touchpoint = new Vector3();

camera.unproject(touchpoint.set(Gdx.input.getX(i),Gdx.input.getY(i),0);
touchedX = touchpoint.x;
touchedY = touchpoint.y;

also i would recommend you to use inputprocessor instead of getting X & Y values from functions Gdx.input.getX(i), Gdx.input.getY(i) as Inputprocesor gives you much more control over multitouch events and you dont have to write the loop for getting touch points it gives you touch pointer as well in its function

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top