mouse click on sprite image rather than on its bounding box

52 Views Asked by At

In all the examples of games, which I examined, written in C++/C# using SFML, .NET Framework, WPF, Python and HTML5 a sprite reacts on mouseClick made within the sprite's bounding box.

Is there a technique in to make the sprite react to clicks made within the sprite image only?

I examined many games, but not a single one exhibit the necessary behaviour

1

There are 1 best solutions below

1
Mike Nakis On

The technique is as follows:

  1. Obtain the mouse click coordinates.
  2. Subtract the sprite coordinates, to obtain sprite-local offsets.
  3. Use the sprite-local offsets to look up the pixel in the sprite.
  4. If the pixel is transparent, then the click was not within the sprite image.