java - Easy way to detect if sprite was touched? -


i have 2 sprites , need touched @ same time made 2 vector2 xy , xy1.

//xy = x , y coordinate of pointer 1 //xy1 = x x , y coordinate of pointer 2  //faces class sprites   if(xy.x >= faces.facespr.getx() && xy.x <= faces.facespr.getx() + faces.facespr.getwidth() &&               //detecting if xy , xy1 inside sprites                     xy.y >= faces.facespr.gety() && xy.y <= faces.facespr.gety() + faces.facespr.getheight() &&         //*                     xy1.x >= faces.facespr1.getx() && xy1.x <= faces.facespr1.getx() + faces.facespr1.getwidth() &&     //*                     xy1.y >= faces.facespr1.gety() && xy1.y <= faces.facespr1.gety() + faces.facespr1.getheight())      //*                 score += 1;             else if(xy1.x >= faces.facespr.getx() && xy1.x <= faces.facespr.getx() + faces.facespr.getwidth() &&        //*                     xy1.y >= faces.facespr.gety() && xy1.y <= faces.facespr.gety() + faces.facespr.getheight() &&       //*                     xy.x >= faces.facespr1.getx() && xy.x <= faces.facespr1.getx() + faces.facespr1.getwidth() &&       //*                     xy.y >= faces.facespr1.gety() && xy.y <= faces.facespr1.gety() + faces.facespr1.getheight())  

i haven't tested yet if code works, there easier , better way of doing it?

edit:

i tried use getboundingrectangle() method , works have problem on setting it's postion.

sprite.getboundingrectangle().setposition(x,y); // check if works system.out.println(sprite.getboundingrectangle().getpostion); 

but result 0.0, 0.0

the solution simple. rectangle of , make contains call.

sprite.getboundingrectangle().contains(new vector2d(touch.x,touch.y)); //or sprite.getboundingrectangle().contains(touch.x, touch.y); // 

make sure unprojected correct.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -