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.

Dark GDK / Function that detects if a sprite was clicked?

Author
Message
Charles Black
16
Years of Service
User Offline
Joined: 9th Mar 2008
Location:
Posted: 16th Nov 2008 03:08
Is there such a function that detects if a sprite was clicked? I couldn't find any in the documentation.

Thanks in advance.
SushiBox
16
Years of Service
User Offline
Joined: 20th Sep 2008
Location: Ohio
Posted: 16th Nov 2008 14:28
What I did, was found the position of the sprite on the screen, width and height, then detected for a mouse click within those 4 points.

www.Helios-Online.net
AndrewT
17
Years of Service
User Offline
Joined: 11th Feb 2007
Location: MI, USA
Posted: 16th Nov 2008 15:44 Edited at: 16th Nov 2008 15:45


^^

That should work, but I didn't have the chance to test it. If you have any problems let me know.

SunDawg
19
Years of Service
User Offline
Joined: 21st Dec 2004
Location: Massachusetts
Posted: 16th Nov 2008 15:44 Edited at: 16th Nov 2008 15:45
That's how I always do it, and it's perfect for rectangular sprites. However, it doesn't work so well if you have sprites with large regions that are invisible, and you don't want the click to register there. Look up per-pixel collision, if that's what you want to do.


My site, for various stuff that I make.
SushiBox
16
Years of Service
User Offline
Joined: 20th Sep 2008
Location: Ohio
Posted: 16th Nov 2008 16:10 Edited at: 16th Nov 2008 16:11
For the project I am working on now, we made a ButtonHandler class that has things like Clicked, Hovered, Delete and etc.

Here is a snippet a lot like Andrews, but this is for mouse hover. Useful if you want to make your image highlight when its hovered before you click.


www.Helios-Online.net
Swordsman
15
Years of Service
User Offline
Joined: 12th Nov 2008
Location: Wigan, England
Posted: 16th Nov 2008 20:27
You could always cheat. Make your cursor a sprite and use dbSpriteCollision() on click events?
I can't think of a reason why this wouldn't work.
Charles Black
16
Years of Service
User Offline
Joined: 9th Mar 2008
Location:
Posted: 17th Nov 2008 00:38
We should ask for a sprite clicked function.

For the while, I'll use AndrewT's. thanks!
SunDawg
19
Years of Service
User Offline
Joined: 21st Dec 2004
Location: Massachusetts
Posted: 17th Nov 2008 01:08
@Swordsman: That does work, until your program inevitably slows down a little, and the cursor's point lags, causing undesired clicks. For most purposes it's still viable, however.


My site, for various stuff that I make.
Swordsman
15
Years of Service
User Offline
Joined: 12th Nov 2008
Location: Wigan, England
Posted: 17th Nov 2008 01:51
Thanks SunDawg,

It's always nice to hear from the guys who have been around GDK longer than I, on little things like this. You'veprobably saved me a couple of hours work somewhere down the line
Jonas
19
Years of Service
User Offline
Joined: 10th Aug 2005
Location: What day is it?
Posted: 22nd Nov 2008 01:06 Edited at: 22nd Nov 2008 01:10
Quote: "You could always cheat. Make your cursor a sprite and use dbSpriteCollision() on click events?
I can't think of a reason why this wouldn't work."


That's how I do it too, but more specifically, I use a hidden 1x1 sprite and keep the mouse or use a custom sprite in place of the cursor.

Quote: "@Swordsman: That does work, until your program inevitably slows down a little, and the cursor's point lags, causing undesired clicks. For most purposes it's still viable, however."


Ouch... my self esteem. Could one just run a check to see if the mouse and the hidden 1x1 sprite are in the same location before allowing a mouse click event to take place? I'm going to go test some solutions.
SunDawg
19
Years of Service
User Offline
Joined: 21st Dec 2004
Location: Massachusetts
Posted: 22nd Nov 2008 01:32
You certainly could, but that will be even slower. The best idea is to have the click register when the mouse button is released, rather than depressed.


My site, for various stuff that I make.
Jonas
19
Years of Service
User Offline
Joined: 10th Aug 2005
Location: What day is it?
Posted: 22nd Nov 2008 03:01
The slow down for that would be marginal and would never increase. It will never run that simple piece of code more than 60 times per second. And actually, if it is lagging behind, it will save resources by not performing the collision detection.
SunDawg
19
Years of Service
User Offline
Joined: 21st Dec 2004
Location: Massachusetts
Posted: 22nd Nov 2008 13:32
Regardless, waiting for the button to be released is usually better. Much more intuitive for menus, anyway. Of course, in actual gameplay, this might not be the desired effect.


My site, for various stuff that I make.

Login to post a reply

Server time is: 2024-09-30 11:19:35
Your offset time is: 2024-09-30 11:19:35