[solved]How do I find out where a sprite was clicked?

Get help using Construct 2

Post » Tue May 17, 2016 12:47 am

I'm working on a battleship game and I want to add explosions when a ship is hit, I've figured out how to do it by spawning an explosion sprite at my cursor but the asymmetry is killing me! What I would like to do is spawn the explosion at set centered points depending on which segment you clicked on. At first this didn't seem too difficult as I know how many segments each boat is and how many pixels so I can easily find out how large a segment is and where it begins and ends, my problem however is the mouse x and y pertain to the whole canvas not just my sprite so there is no way I can just compare mouse coordinates to my height.
Last edited by chris200x9 on Tue May 17, 2016 6:07 pm, edited 1 time in total.
B
15
S
7
G
1
Posts: 34
Reputation: 1,793

Post » Tue May 17, 2016 3:33 am

my guess:
you could create invisible markers along the boat using containers and use pick nearest to spawn the explosion sprites exactly where you want
B
22
S
4
G
3
Posts: 61
Reputation: 2,683

Post » Tue May 17, 2016 3:45 am

Check it against the boat's bounding box

when you touch the boat, you can get boat.bboxleft (as your starting X point) and bboxright or width for the right edge, same goes with top to bottom.

ex, for a 256 width boat with 4 equal 64px parts

just do on touch/click

ceil( (mouse/touch.X - boat.bboxleft ) / 64 )

to get a 1 to 4 result
Image
B
79
S
29
G
12
Posts: 515
Reputation: 12,584

Post » Tue May 17, 2016 6:07 pm

Lof wrote:Check it against the boat's bounding box

when you touch the boat, you can get boat.bboxleft (as your starting X point) and bboxright or width for the right edge, same goes with top to bottom.

ex, for a 256 width boat with 4 equal 64px parts

just do on touch/click

ceil( (mouse/touch.X - boat.bboxleft ) / 64 )

to get a 1 to 4 result


Brilliant! Thank you.
B
15
S
7
G
1
Posts: 34
Reputation: 1,793


Return to How do I....?

Who is online

Users browsing this forum: diamantico21, ratboy3 and 38 guests