How do I automate "Z Order = Y Position?"

    I feel like I'm overlooking a basic feature of Construct 2, but I can't seem to find the info anywhere on this.

    Is there a way to set the Z Order of sprites on a layer relative to their Y position? I'm trying to have an isometric game where humanoid sprites can walk in front of and behind each other

    Yesterday I pulled this off, by using some information from the forums here.

    I am doing this from work, so I might make a mistake, and someone please correct me if I am wrong.

    I first gave the sprite an instance variable. (this is going to be the y-value of the sprite)

    I made an event with condition: order by (that instance variable)

    And the action was: sent to top?

    I also just found this -->

    'For each ordered' is a good way - it's not CPU intensive if it's not nested with any other loops.

    Sort by Y co-ordinate:

    + For each Sprite order by Sprite.Y ascending

    -> Sprite: send to front

    Sort by any old number:

    + For each Sprite order by Sprite('some_variable_with_z_number')

    -> Sprite: send to front

    Then you can set any old number to the 'some_variable_with_z_number' private variable, and it'll sort the Z order from that.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads

    That is the 'old' method.

    Now days you have a system action: Sort Z order

    Sort the Z order of instances of a given object according to an instance variable. This effectively removes all instances from their Z order, sorts them, then inserts the sorted sequence back in to the holes left behind. This means if the instances are spread across a range of layers, they will be sorted in the same Z order locations, maintaining the same order relative to other instances on those layers. Note this action is much faster than using an ordered For each with an action like Send to front/back, so this action should be the preferred way to sort the Z order of large numbers of instances.

    Basically you store the y in an instance variable (in a loop) and sort with that variable.

    But. I found (for automation) the rexrainbow plugin much faster:

    Thanks for the quick responses. I hope this can help me efficiently make something like the classic RPG towns of old.


    I did not compare the performance of my z-sort plugin with "system action: sort Z order", sorry I could not promise which one is better.

    Hmm, if "Sort Z Order" only works for instances of one particular sprite, then I guess instead of using a Family of NPCs, monsters, and town objects, I'll have to turn all characters, buildings, and other such things into a single sprite object with different animations, or frames of animation to identify them.

    Is this really the easiest solution?

    Families have variables.

    That's a relief. It'd seem like a pretty major oversight if we couldn't make a game from an isometric perspective where sprites could be drawn going in front of and behind each other dynamically.

    I feel like I'm overlooking a basic feature of Construct 2, but I can't seem to find the info anywhere on this.

    Is there a way to set the Z Order of sprites on a layer relative to their Y position? I'm trying to have an isometric game where humanoid sprites can walk in front of and behind each other

    This is my solution using a loop method already in Construct 2:

    + For each Family order by Family.Y ascending
    -> Family: send to front[/code:1e81tt2n]
    Make a family "foo" and add sprites into that family.  Sprites in those family will have to be in the same layer in order to pass in front and behind one another.
    I'm sure there're other methods but this is the easiest one.  Maybe use this one as a prototype and see if it works for what you have.  Then as you move on and your sprite movement get more complex, you can play around with some other looping method.
    If your sprites have simulated depth, you'll just have to offset the origin point for that sprite.  For example, if you have a bridge, and since there is no Z in 2D, technically the bridge section is just placed Y+100 from its origin.


    Thats so simple and effective! Thx a lot...

    This thread is a few years old and is now out of date. You should use the System 'Sort Z order' action, which is significantly faster.


    Hey Ashley, thanks for that Information, basically i tried it out but it doesnt work that good like the other post.

    I did:

    Condition: For each >Family Object< (different sprites i have to sort) Action: Set zOrder(var) to Self.Y

    -> -> -> Child Action: Sort >Family Object< Z order by zOrder(var)

    It works partically, but sometimes the sorting is wrong.

    Best regards

    The system 'Sort Z order' action works the same as the way using for-each ordered, but much faster.

    If you were using 'For each Family order by Family.Y ascending: send to front', instead use 'Every tick: set Family sortOrder to Family.Y, sort by Z order by variable sortOrder'.

    For anyone who is still as confused as I was after reading this thread, here is a step by step as to how I handled it.


    I created a user-controlled character called "Player"

    I created an isometric obstacle called "Tree" in the same Layer as "Player"

    Next, I edited the collision on "Player" to only collide around the character's feet.

    Then I edited the collision around the "Tree" to only collide around the base of the tree.


    I created a new Family called "Sprite" and added both "Player" and "Tree" to it.

    I selected "Sprite" from the right-side menu and then clicked "Add / edit Family instance variable" from the left-side menu.

    After clicking "Add new instance variable" from the popup, I named the variable "sortOrder" and type "Number" with the initial value of 0.


    Next, I visited the Event Sheet where I added a new System action for "Every tick."

    To the right of that, I added a an action where I first selected "Sprite" and then "Set Value" to "Sprite.Y"

    Immediately below that, I added a new System action followed by "Sort Z Order" which asks which Object to select. I chose "Sprite" and the "sortOrder *number)" variable was already selected as the instance variable.

    That's all. I clicked play and everything sorted properly. NOTE: I had to play with the collisions on both the Player and Tree object to get them to look the way I wanted.

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