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 / Sprite selection by depth

Author
Message
MateiSoft
6
Years of Service
User Offline
Joined: 22nd Oct 2012
Location:
Posted: 17th Dec 2018 23:24
Hello community,

I want to select the sprites that are overlapping but select the one that stands in front of the other one.

I have an example here with two sprites overlapping but when I place my cursor between, it selects both of them. How can I select the one that stands forward?

Thank you!

www.alexmatei.com
Jeff Miller
14
Years of Service
User Offline
Joined: 22nd Mar 2005
Location: New Jersey, USA
Posted: 18th Dec 2018 00:36
My suggestion: in your loop, test the depth of each sprite hit, using the GetSpriteDepth() expression, and select the sprite with the lower (nearer) depth.
MateiSoft
6
Years of Service
User Offline
Joined: 22nd Oct 2012
Location:
Posted: 18th Dec 2018 00:51
Jeff,

Thank you for your suggestion!

I thought about that but that doesn't mean that it will select JUST the sprites with the lower depths?
www.alexmatei.com
Jeff Miller
14
Years of Service
User Offline
Joined: 22nd Mar 2005
Location: New Jersey, USA
Posted: 18th Dec 2018 01:04
Your program currently selects (acts by drawing a box) all sprites hit. You need to test all sprites hit and then have the program "select" (for whatever purpose) the one sprite with the lower depth. So store the hits and corresponding depths inside the loop, but make the selection based on the nearer depth after the end of the loop. I think that's what you want.
MateiSoft
6
Years of Service
User Offline
Joined: 22nd Oct 2012
Location:
Posted: 18th Dec 2018 14:10 Edited at: 18th Dec 2018 14:10
I've done some spaghetti code, but 100% working! Have a look, please! And if someone needs the code, here it is:

www.alexmatei.com
MateiSoft
6
Years of Service
User Offline
Joined: 22nd Oct 2012
Location:
Posted: 19th Dec 2018 20:46
I just noticed that it works only on two of the sprites, but when I add the third one, the code breaks and it doesn't do what it supposed to... Can someone help me achieve the main goal, please?

Big thank you!
www.alexmatei.com
Jeff Miller
14
Years of Service
User Offline
Joined: 22nd Mar 2005
Location: New Jersey, USA
Posted: 19th Dec 2018 22:43
In the loop,

If the iteration variable is the minimum value for the variable, set a new variable depthmin = to the tested depth value. Set another new variable called sprmin = the value of the iteration variable.

If the iteration variable is above the minimum value for the variable, check for depth of the sprite associated with the iteration variable. If it is nearer depth than depthmin, set depthmin equal to the nearer depth and set sprmin = to the new iteration variable value.

After the loop completes, sprmin will represent the nearest of the sprites which were hit.
MateiSoft
6
Years of Service
User Offline
Joined: 22nd Oct 2012
Location:
Posted: 21st Dec 2018 12:37
Your explanation is good, however I can't seem to make it work... I can't imagine how it can be done. Is is possible, please, to reproduce in the code provided a few posts ago, your explanation?

Thank you! You've been really helpful!
www.alexmatei.com
JosephB
12
Years of Service
User Offline
Joined: 12th Sep 2006
Location:
Posted: 22nd Dec 2018 01:45
How about the following using arrays:

MateiSoft
6
Years of Service
User Offline
Joined: 22nd Oct 2012
Location:
Posted: 22nd Dec 2018 23:45
Josepth B,

Thank you so much for the example!

To select just the sprite from the top of the collided sprites, what should I modify?
www.alexmatei.com
JosephB
12
Years of Service
User Offline
Joined: 12th Sep 2006
Location:
Posted: 25th Dec 2018 00:00
Sorry MateiSoft to reply late, but I do not check the forums often during the holidays. I did not include any code for using the mouse buttons to select any specific sprite. The code automatically draws a box around the top sprite when two or more sprites are colliding at the cursor location (really just collision of sprite number one with the other sprites). The code does not determine if a sprite is colliding with another sprite if the cursor is at a location on a sprite where only one sprite is detected because at that point there is no other sprite. That sprite could be colliding with another sprite at another location.
MateiSoft
6
Years of Service
User Offline
Joined: 22nd Oct 2012
Location:
Posted: 3rd Jan 2019 11:53
Is it possible to show me how can I select just the sprite that is on top of other sprite? I am stuck at doing this for multiple arrays of sprites like: If I have an array of 2 sprites one on top of each other it will work, but if I have another array of 2 sprites on top of each other, it will break.

Thank you so much!
www.alexmatei.com
JosephB
12
Years of Service
User Offline
Joined: 12th Sep 2006
Location:
Posted: 4th Jan 2019 03:35
The following code example will create three sprite arrays of three sprites for each array. The code will select the sprite with the lowest sprite depth number (closer to top of the screen for all groups of over lapping sprites no matter which array they belong to. I have tried to keep it straightforward and included some comments. I hope it is helpful. It seems to work for me, but I am not sure exactly what you would like. I also included some code to swap the sprite depth of the top sprite with a sprite in the collision group that has a higher sprite depth number.

MateiSoft
6
Years of Service
User Offline
Joined: 22nd Oct 2012
Location:
Posted: 6th Jan 2019 14:12
Josepth B,

That's exactly what I wanted!

What's with the + 1 on the for loops?

Thank you!
www.alexmatei.com
JosephB
12
Years of Service
User Offline
Joined: 12th Sep 2006
Location:
Posted: 6th Jan 2019 19:37
MateiSoft,

I am glad it was helpful. For this code example the sprcount + 1 is used to keep track of assigning an unused sprite number to each sprite in each array of sprites. It starts in the first For/Next loop and continues through for each array. This ensures that a sprite number is not used twice. I usually like to assign the IDs on my own. On line 263, you should delete the part "+ Str(frontspr) + " " + Str(frontsprdepth)" as that is only there for debugging when I was writing the code.

Login to post a reply

Server time is: 2019-06-19 06:58:05
Your offset time is: 2019-06-19 06:58:05