Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm not sure if this is at all possible, but maybe it will spark up an interesting conversation. (Sidenote: working on a small game in Löve).

I'm making an archery shooting kind of game, and I was thinking it might be cool to have critical and regular hits based on where the user shoot the arrow at the enemy. Example, if I have a troll image, and the user clicks the head of the troll, it will count as a critical hit.

Checking to see if the user clicked on the top part of the image vs the bottom, isn't the tricky part. The hard bit will be to check if the user clicked on apart of the image, instead of blank space in the png...

So, main question: Is it possible to get the rgb value of where an image has been clicked, to determine if they hit or missed the target.

Also, if this is at all possible, is there anyone familiar with Löve that can confirm it is doable?

Edit: I'm realizing now it's not the rgb value I want to look for, it is whether or not the clicked pixel has transparency. If it is 100% transparent that means the user missed the image.

share|improve this question
    
I think the question you should be asking is how assign and detect hit regions on an image. If you're depending on the RGB value, you limit yourself to a range of colors that can only be shown at the critical parts (like requiring the head to be a different color than the neck). Additionally, the hit regions are far more re-usable for other enemies. –  Byte56 Jan 7 '13 at 22:35
    
I see what you're saying, but that's not exactly what I'm trying to say. If the rgb value of a click is nothing, that means it was a miss. So I would use the value to determine if the point is even apart of the actual image or not. –  mbreen Jan 7 '13 at 22:40
add comment

1 Answer

up vote 3 down vote accepted

You can get specific information about the image clicked by accessing the ImageData associated with the Image you're drawing. Specifically the getPixel function. When you detect a click, find which image is being clicked on. Then you need to get the ImageData you created for that image. Finally, convert the click coordinates to image local coordinates and pass them into the getPixel(x, y) function. You'd likely want to test the alpha component to see if it's below whatever threshold you define as "not part of the image".

share|improve this answer
    
"Specifically the getPixel function" - awesome! This is exactly what I was looking for, and now I feel foolish for not being able to find that in the wiki. Thank you very much. I'll test this out and comment back in a bit. –  mbreen Jan 7 '13 at 23:02
    
Yup, works perfectly. Thanks for your time. –  mbreen Jan 8 '13 at 0:07
add comment

Your Answer

 
discard

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.