Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Adding clickable regions to Sprites

Author
Message
badkneecap
13
Years of Service
User Offline
Joined: 21st Jun 2010
Location:
Posted: 15th Jun 2020 03:20
Is there an easy way (or formula) to test if a mouse was clicked in a certain region on a sprite? For example, I have an image that is 100 x 100. If the user clicks anywhere in the square from 0, 0 to 49, 49 I take them to graphic 1. If they click anywhere in the square 50, 0 to 100, 50 I take them to graphic 2, etc. I also scale my images to fit the width of my screen, so it could be twice the size. I was looking for a function that would return my mouse pointer position X and Y relative to the X and Y position of the top left of the sprite. Does that make sense? Is there a function that would return that or is there a function that calculates this already?
Scraggle
Moderator
20
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 15th Jun 2020 03:47 Edited at: 15th Jun 2020 03:47
You can use GetSpriteInBox() and offset the coordinates from the sprites position.
Bengismo
6
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 15th Jun 2020 07:06 Edited at: 15th Jun 2020 09:56
You can use GetSpriteHit() and GetSpriteXFromWorld( iSpriteIndex, x, y ) /GetSpriteYFromWorld( iSpriteIndex, x, y ) to find out where on the sprite it was clicked relative to the current sprite offset.


Alternatively, you could set the sprite up with 4 different box shapes and set each shape with a different sprite category.

Then you just use getspritehitcategory() to find out which (if any) of the 4 regions were clicked on. Its harder to set up but its then nice and easy to use.

Login to post a reply

Server time is: 2024-04-19 13:33:31
Your offset time is: 2024-04-19 13:33:31