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.

DarkBASIC Professional Discussion / Clicking on a sprite

Author
Message
Qoheleth
15
Years of Service
User Offline
Joined: 11th Jun 2011
Location:
Posted: 2nd Oct 2011 13:26
I posted this question in the 2D forum, but I suppose that was the wrong place. I've searched, but could not find an answer to what I thought might be a very common question. How do I tell DBP to determine if I clicked on a specific sprite? Using MouseX, MouseY and MouseClick are easy *if* your sprites are rectangles, but gets complicated if the shapes are irregular and you have to use mathematical formulas to determine if the click happened somewhere inside the confines of the sprite.
noobnerd
15
Years of Service
User Offline
Joined: 30th Nov 2010
Location:
Posted: 2nd Oct 2011 20:24
Im no pro but it should be possible.

for circles easy but that doesnt seem to be what you are after.

you could try to:



if you have a non black background you will have to paste the sprite like this:



hope it helps
noobnerd
15
Years of Service
User Offline
Joined: 30th Nov 2010
Location:
Posted: 2nd Oct 2011 20:30 Edited at: 2nd Oct 2011 20:31
oh and here is a better version, one that uses a function

just call the mouseonsprite(sprite number) function anywhere in your code and it will return a 0 if the mouse is not on the sprite, a 1 if it is on the sprite and a 2 if it is on the sprite and you clicked it.

call it like this:



if mouseonsprite(number) = 1 then ...
if mouseonsprite(number) = 2 then ...

or like this

clicked = mouseonsprite(number)
if clicked = 1 then ...
if clicked = 2 then ...

iff you have problems or dont know how it woeks just ask i will try to ansvear
WickedX
17
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 3rd Oct 2011 02:08 Edited at: 3rd Oct 2011 06:03
The attached download is my take on the solution to this problem. The program loads an irregular shaped image for the sprite. I believe I’ve covered all variables backdrop color, background colors and sprite offsets and what not. Feedback is welcome should anyone fined a flaw in this routine.

Edit: Just noticed the ink command uses ianm’s plugin. Source had been modified to use DBPro’s ink command.
Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 3rd Oct 2011 05:33 Edited at: 3rd Oct 2011 05:34
Quote: "Using MouseX, MouseY and MouseClick are easy *if* your sprites are rectangles, but gets complicated if the shapes are irregular and you have to use mathematical formulas to determine if the click happened somewhere inside the confines of the sprite."


Actually all sprites are rectangles... even the transparent areas count as part of the sprite. The quick way is to have a 1x1 sprite at the mouse coordinates and use the SPRITE COLLISION() command to check if the sprite at the mouse coordinates is colliding with another sprite.



noobnerd
15
Years of Service
User Offline
Joined: 30th Nov 2010
Location:
Posted: 3rd Oct 2011 10:49
im not sure but i thin he wants to know when the mouse is visually above the sprite, not on the transaparent background next to it.
Pincho Paxton
23
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 3rd Oct 2011 15:09
One of the plugins uses shaped sprites, but I can't remember which one.

SimpleProgram
14
Years of Service
User Offline
Joined: 2nd Oct 2011
Location: Cali
Posted: 3rd Oct 2011 21:43
Just remember to make the new mouse sprite have a high priority so that it does not get drawn under another sprite:


Sprite priority mousesprite, 5

good luck.

Simple knows best.
zenassem
23
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 5th Oct 2011 02:38
This is just theory, but couldn't you have a mousemap of the sprite and then check against this to see if you're on a visible part of the sprite?

By mousemap I am referring to an image mask. The mask would be 2 colors say black and white, one color representing the visible part of the sprite. Now the image could either be an actual image or a memblock (being that the POINT command is rather sluggish IIRC). If you know where the sprite is postioned (specifically calculating the left corner and it's width & height)and where the mouse is positioned, you could calculate the offset say into the memblock, and check the corresponding value.

For the most part I find that I don't need pixel perfect detection, and some of the methods described work well enough for me. But if I needed to be very precise, I would think I could achieve it by the above.

Your signature has been erased by a mod please reduce it to 600 x 120.
Qoheleth
15
Years of Service
User Offline
Joined: 11th Jun 2011
Location:
Posted: 5th Oct 2011 16:38
Sorry, I've been out commission for a few days. Thanks to everyone who answered. I have to carefully look at the codes posted - I'm still new to the graphic stuff. This is all except for zenassem's post, which I don't understand at all.
In the 2D forum, where I originally posted, Simple gave a simple solution to use a sprite for the mouse and then use sprite hit(). I tried that and it seems to work.
Qoheleth
15
Years of Service
User Offline
Joined: 11th Jun 2011
Location:
Posted: 5th Oct 2011 16:54
WickedXGames, I downloaded your code and tested it. It certainly works. Then I stared at the code for a few long minutes. Then I stared at it some more. Then I decided to tell you: I've got no idea what you did.
zenassem
23
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 5th Oct 2011 17:23 Edited at: 5th Oct 2011 17:37
Quote: "In the 2D forum, where I originally posted, Simple gave a simple solution to use a sprite for the mouse and then use sprite hit(). I tried that and it seems to work."


That will work, but it does not handle the fact that the transparent area of the sprite will also trigger a collision. Everyone in this thread assumed you meant that the bounding rectangle shouldn't count, and that you needed near pixel perfect collision for non-rectangular sprites. But if it works for you then all is well.

I will have to work up an example to probably have a mousemap make sense, for those that haven't worked with a mousemap before. A similar issue occurs in Isometric games, because a rectangular area being clicked on, in an isometric tilemap, could actually be any 1 of 5 tiles.

hence the following image is used to determine which tile the user is selecting


In the above image, each color represents either part of a neighboring Iso tile (the 4 corners) and a full ISO tile (the white area) in the middle. By looking up what color is under the mouse pointer, you can determine which tile is actually being selected.

Probably still doesn't make sense without a functioning example; for people who haven't attempted an isometric tilemap.? :-|

Your signature has been erased by a mod please reduce it to 600 x 120.
Qoheleth
15
Years of Service
User Offline
Joined: 11th Jun 2011
Location:
Posted: 5th Oct 2011 18:29
Quote: "Everyone in this thread assumed you meant that the bounding rectangle shouldn't count, and that you needed near pixel perfect collision for non-rectangular sprites."

Yes, you are right. I did not consider the bounding rectangle. I am going to figure out Wicked's code.
To try and make myself more clear: I want a number of images (sprites) on the screen (in the present case 76) and then determine on which one the user clicked. As they are close to each other I will have to take the bounding rectangle into account.
noobnerd
15
Years of Service
User Offline
Joined: 30th Nov 2010
Location:
Posted: 5th Oct 2011 19:31
my function does that. didnt it work?
WickedX
17
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 6th Oct 2011 06:04
Qoheleth, I have added comments to the function in the snippet. Hopefully this with the DBPro help file (lol) will help to make it clearer.

Login to post a reply

Server time is: 2026-07-10 23:33:03
Your offset time is: 2026-07-10 23:33:03