How to track multiple touch events in Libgdx?

I am making a racing game using Libgdx. I want to touch half of the right side of the screen to speed up, at the same time, without deleting the previous touch point, tap again on the left side of the screen to shoot. I cannot detect later touch points.

I searched and got the Gdx.input.isTouched(int index) method, but cannot determine how to use it. My screen code:

 if(Gdx.input.isTouched(0) && world.heroCar.state != HeroCar.HERO_STATE_HIT){ guiCam.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0)); if (OverlapTester.pointInRectangle(rightScreenBounds, touchPoint.x, touchPoint.y)) { world.heroCar.state = HeroCar.HERO_STATE_FASTRUN; world.heroCar.velocity.y = HeroCar.HERO_STATE_FASTRUN_VELOCITY; } } else { world.heroCar.velocity.y = HeroCar.HERO_RUN_VELOCITY; } if (Gdx.input.isTouched(1)) { guiCam.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0)); if (OverlapTester.pointInRectangle(leftScreenBounds, touchPoint.x, touchPoint.y)) { world.shot(); } } 
+7
source share
1 answer

You want to use the Gdx.input.getX(int index) method. The integer index parameter represents the identifier of the active pointer. To use this correctly, you will want to repeat all possible pointers (in case two people have 20 fingers on the tablet?).

Something like that:

 boolean fire = false; boolean fast = false; final int fireAreaMax = 120; // This should be scaled to the size of the screen? final int fastAreaMin = Gdx.graphics.getWidth() - 120; for (int i = 0; i < 20; i++) { // 20 is max number of touch points if (Gdx.input.isTouched(i)) { final int iX = Gdx.input.getX(i); fire = fire || (iX < fireAreaMax); // Touch coordinates are in screen space fast = fast || (iX > fastAreaMin); } } if (fast) { // speed things up } else { // slow things down } if (fire) { // Fire! } 

An alternative approach is to configure the InputProcessor to receive input events (instead of "polling" the input as the above example). And when the pointer enters one of the areas, you will need to track this state of the pointer (so that you can clear it if it remains).

+12
source

All Articles