Search code examples
libgdxtouch

Touch on an object and score increment does not working


I have an array of coins.I increment the score on touch of a coin.

    if (Gdx.input.isTouched()) {
        Vector3 touchPos = new Vector3();
        touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
        game.camera.unproject(touchPos);
        for (int i = 0; i < coins.length; i++) {
            Rectangle textureBounds = new Rectangle(coins[i].getX(), coins[i].getY(), coins[i].getWidth(),
                    coins[i].getHeight());

            if (textureBounds.contains(touchPos.x, touchPos.y)/* && !coins[i].isTapBool()*/) {
           System.out.println("touched");

                coins[i].setTapBool(true);
            }
        }
    }

Increments score like this:

for (int i = 0; i < coins.length; i++) {
        if (coins[i].isTapBool()&&coins[i].isCoinVisible()) {

            coinScoreController.updateCoinScore(delta);//score=score+1
            coins[i].setTapBool(false);
        }
    coins[i].isTapBool());
    }

Now each time when I touch a coin score increments by 13 not 1.The print statement inside the if condition for tap also prints 13 times. I want to increment score by 1 only. How to solve this?What I did wrong in the code?


Solution

  • I got it working after changing Gdx.input.isTouched() to Gdx.input.justTouched().