简体   繁体   中英

How to detect if two sprites are being touched at the same time?

I tried this:

if(spr.getBoundingRectangles.contain(x,y)){
    //do this
}

but how to detect if the other sprite is touched by the second pointer?

EDIT:

for(int i = 0; i < Constants.MAX_POINTERS; i++){
           if(Gdx.input.isTouched(i)){
               xy.set(Gdx.input.getX(i), Gdx.input.getY(i), 0);
               xy1.set(Gdx.input.getX(i), Gdx.input.getY(i), 0);
               WorldRenderer.camera.unproject(xy);
               WorldRenderer.camera.unproject(xy1);

               if(Spr.getBoundingRectangle().contains(xy.x, xy.y) &&
                       Spr1.getBoundingRectangle().contains(xy.x, xy.y))
                   score += 1;
           }
       }

What happens is that xy and xy1 are always the same, when I touch the screen with the second pointer they will just both switch to the new coordinates instead of having two different x,y for both xy and xy1.

you can iterate over all pointers checking whether they are touching the screen and then check if positions overlapping sprites position

    final int MAX_POINTERS = 5;

    ...

    for(int i = 0; i < MAX_POINTERS; i++)
    {
        if( Gdx.input.isTouched(i) )
        {
            int x = Gdx.input.getX(i);
            int y = Gdx.input.getY(i);

            if( sprite.getBoundingRectangle().contains(x, y) ) //instead of checking one sprite iterate over sprites array
            {
                System.out.println("The sprite is touched!");
            }

            //if... - or just add more ifs
        }
    }

you need to define max count of pointers to iterate over it - as far as I know Libgdx supports up to 20 pointers


ABOUT EDIT:

Of course they are the same... :) You are placing the same value to vetors . My example above is far more generic that you need - if you know that you have two pointers you can just use:

if( Gdx.input.isTouched(0) && Gdx.input.isTouched(1) ) //because if two pointers are touching screen there is a chance that they are touching two sprites
{
    xy.set(Gdx.input.getX(0), Gdx.input.getY(0), 0);
    xy1.set(Gdx.input.getX(1), Gdx.input.getY(1), 0);

    //checking if pointer 1 is touching sprite 1 and pointer 2 is touching sprite 2 OR VICE VERSA
    if( (Spr.getBoundingRectangle().contains(xy.x, xy.y) && Spr1.getBoundingRectangle().contains(xy1.x, xy1.y))
        ||
        (Spr.getBoundingRectangle().contains(xy1.x, xy1.y) && Spr1.getBoundingRectangle().contains(xy.x, xy.y)) 
      )
    {
        score += 1;
    }
}

or just create the function that will return true if all sprites you will pass to it are touched (which actually will can handle more than two sprites)

boolean allTouched(Array<Sprite> sprites)
{
    int spritesCount = sprites.size;
    int spritesTouched = 0;

    for(int i = 0; i < MAX_POINTERS; i++)
    {
        if( Gdx.input.isTouched(i) )
        {
            for(Sprite sprite : sprites)
            {
                if( sprite.getBoundingRectangle().contains(Gdx.input.getX(i), Gdx.input.getY(i)) )
                {
                    spritesTouched++;
                    sprites.removeValue(sprite, true); //to not doubling the same sprite
                }
            }
        }
    }

    return spritesCount == spritesTouched ;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM