How do I manage Zorder between same instances?

0 favourites
  • 7 posts
  • 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

  • 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

  • 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):

  • 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!


    • 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


    • 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!!!!!

  • 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.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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.

  • Hurrah!! Glad to help .

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)