Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to click on the particular sprite from the sprite sheet. For instance i have fruits sprite sheet i want to click on the particular fruit.

Thanks, Jubin Mehta

share|improve this question
up vote 1 down vote accepted

A general solution would be:

  1. Get the cursor position.
  2. Transform the position into the tiles local coordinate space.

    pixelPositionX = cursorPositionX - (tilePositionInWorldX - screenPositionX)

    pixelPositionY = cursorPositionY - (tilePositionInWorldY - screenPositionY)

  3. Find out if the position is inside the tile.

    tilePositionOnSpritesheetX < pixelpositionX < tilePositionOnSpritesheetX + tileSizeX

    tilePositionOnSpritesheetY < pixelpositionY < tilePositionOnSpritesheetY + tileSizeY

  4. If it is, check if the alpha value of the pixel at that position is over a certain treshold.

Step 4 is optional, without it, you're checking against the tiles bounding rectangle, with it, clicking is pixel perfect.

Please note that all positions and sizes are assumed to be in pixels.

share|improve this answer
Just found a related question that goes into the implementation details for cocos2d:… – sarahm Dec 21 '11 at 4:18

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.