libGDX touch in effect after touch

I use the following code to match the user touch to a ball object's position, so when the user touches the ball it bounce back up. Code:

int x1 = Gdx.input.getX();
int y1 = Gdx.input.getY();
Vector3 input = new Vector3(x1, y1, 0);
cam.unproject(input);

if(ball.getBoundingCircle().contains(input.x, input.y)) {
ballBounce();
}

But I have a problem with the touch. If the user touches a certain position on screen for a moment and a ball (after a while more appear) about to reach the position the user touched, the ball will recognize itself as been touched and the ballBounce(); method will start and continue with other balls that reach to the same position until the user touch another position on the screen but then that position will be fixed till the new one... Do someone know how I can bypass that problem so if the user stop touching the screen then where heshe touched won't affect the ball objects?


It seems that you are not using any input processor. Make your class implement InputProcessor and make your touchDown method look like this.

@Override
touchDown(InputEvent event, float x, float y, int pointer, int button) {
    Vector3 input = new Vector3(x, y, 0);
    cam.unproject(input);

    if (ball.getBoundingCircle().contains(input.x, input.y)) {
        ballBounce();
    }
}

ClickListener (LibGDX API)

链接地址: http://www.djcxy.com/p/66444.html

上一篇: 一旦数组中的最后一个元素符合条件,如何使循环结束?

下一篇: 触摸后libGDX触摸有效