0 Favourites

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

  • 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.

  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • 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

  • 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

  • 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.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)