Best way to detect the touched object (moving) from collection in libgdx

后端 未结 2 457
天命终不由人
天命终不由人 2021-01-14 12:33

This is my first attempt in game development. I just started experimenting libgdx and understanding the different aspects of game programming. I looked at the sample project

2条回答
  •  星月不相逢
    2021-01-14 13:17

    I've solved the problem by making an invisible rectangle which will follow the tap location and I made an if statement checking for overlapping of the invisible rectangle and checking for if the screen was just tapped.

    Here is the code:

    if(Gdx.input.isTouched()) {
            Vector3 pointerPos = new Vector3();
            pointerPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
            cam.unproject(pointerPos);
            touchPos.x = pointerPos.x /*+ 64 / 2*/;
            touchPos.y = pointerPos.y /*+ 64 / 2*/;
        }
    
        Iterator iter = raindrops.iterator();
        while(iter.hasNext()) {
            Rectangle raindrop = iter.next();
            raindrop.y -= 300 * Gdx.graphics.getDeltaTime();
            if(raindrop.y + 64 < 0) {
                iter.remove();
            }
            if(raindrop.overlaps(touchPos) && Gdx.input.justTouched()) {
                iter.remove();
                dropSound.play();
            }
    

    all of this code is in the render method.

    P.S: Change raindrops to the fly objects you want

提交回复
热议问题