How do I manage Zorder between same instances?

Get help using Construct 2

Post » Wed Aug 13, 2014 8:21 pm

Hi everyone,

I usually use UID, Zorder without any problems but for this one, I have no clue how to do it.

I would like to manage the Zorder between the same instances that are spawned each X sec.

If Sprite.Y > Sprite.Y ==> Move on top of Object Sprite
If Sprite.Y < Sprite.Y ==> Move on behind of Object Sprite

The issue, as you may probably guess, is I don't know how to get the correct Sprite.UID and even then, I'm not sure it is the correct way (duh)

I usually do the following but it does not work and it is normal that it does not:

Pick by evaluating Sprite.UID
Compare Sprite.UID is different than Sprite.UID
Sprite.Y > Sprite.Y ===========================> Move behind Sprite

I know that it is wrong but I don't find the solution after many hours of testing a few different approaches :(
B
26
S
10
G
3
Posts: 72
Reputation: 4,008

Post » Wed Aug 13, 2014 9:01 pm

Btw, I also tried the technique where I linked another Sprite called "UP" to the "SPRITE"

If "UP" is overlapping "SPRITE"
"UP.vrUID" is different than "SPRITE.vrUID"
"UP.Y">"SPRITE.Y" =========================> "SPRITE" move to bottom of layer
"UP.Y"<"SPRITE.Y" =========================> "SPRITE" move to top of layer

This technique works but does not solve the issue because if I have a "SPRITE" that is between 2 "SPRITE"(s), he will always be on Top of the layer instead of being in front of the "SPRITE" that is <SPRITE.Y and behind the other "SPRITE" that is >SPRITE.Y
B
26
S
10
G
3
Posts: 72
Reputation: 4,008

Post » Wed Aug 13, 2014 10:44 pm

If I understood you correctly, each time you create a new Sprite you could run something like this (as a function or add to the create event):
You do not have the required permissions to view the files attached to this post.
I only occasionally visit - I'm learning C# for Unity, but c2 is still a respectable game engine imo....
B
73
S
19
G
66
Posts: 2,198
Reputation: 42,193

Post » Wed Aug 13, 2014 11:06 pm

Hi @Colludium

thank you but by default, C2 will put the new sprite atop of the older ones. Since I have several sprites that can be spawned randomly on Y, the issue will be that sprites that are supposed to be behind some of other sprites will end up in front.

However, after hours of try and error, I finally found a solution that I will share with everyone, in case it could help others...
Don't hesitate to ask me a Tuto if you need it!

First
- Create one sprite that called "RAIL" and duplicate it how many times you would like on the Y axis.
- On your Sprite (enemies for example), add the variable RAILUID

Second:
- Spawn your sprite enemies by picking a random "RAIL" ==> On sprite enemy, Set Enemy.RAILUID to RAIL.UID

Third and finally :
- For Each Enemy

- If Enemy.RAILUID = 73 (for example) ==> Move Enemy behind RAIL
- System Pick RAIL where RAIL.UID = 74 ==>

Increment it how many times you have RAILS
- If Enemy.RAILUID = 74 ==> Move Enemy behind RAIL
- System Pick RAIL where RAIL.UID = 75==>

Enjoy! Joy! and Freedom!!!!! :)
B
26
S
10
G
3
Posts: 72
Reputation: 4,008

Post » Wed Aug 13, 2014 11:43 pm

@Underdog, maybe I misunderstood what you wanted to do. If you want the sprites that are lower down the screen to appear z order higher than those that are higher up the screen then that one line of code is all you need to achieve it and your way seems quite complicated... If it's not what you wanted to do then I am just being confused. There is a potential gotcha if you use that code, you need a Pick All first if you're implementing it as a sub event straight after your create sprite event; or you could put the For Each loop in a function and call the function after the create sprite event.
I only occasionally visit - I'm learning C# for Unity, but c2 is still a respectable game engine imo....
B
73
S
19
G
66
Posts: 2,198
Reputation: 42,193

Post » Wed Aug 13, 2014 11:57 pm

@Colludium, oh my god!!!!
Your way is the correct one :)

I gave it a try and I realized that my way was way too much complicated after testing yours :)
I never used "For Each (by ordered)" and I guess, I will use it more often now.

Thank you Colludium, I'm really grateful for your help.
B
26
S
10
G
3
Posts: 72
Reputation: 4,008

Post » Wed Aug 13, 2014 11:59 pm

Hurrah!! Glad to help :).
I only occasionally visit - I'm learning C# for Unity, but c2 is still a respectable game engine imo....
B
73
S
19
G
66
Posts: 2,198
Reputation: 42,193


Return to How do I....?

Who is online

Users browsing this forum: NN81, TsubaRu, Yahoo [Bot] and 9 guests