0 Favourites

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.

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

    http://c2rexplugins.weebly.com/rex_zsorter.html

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

  • 99Instances2Go

    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?

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • 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.
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)