Using For (Ordered) to Sort by Y Position Not Working

0 favourites
  • 3 posts
From the Asset Store
Vector illustration of a UFO with an alien in three different positions.
  • GOAL

    To sort instances of the enemy Hunters and the main character, Buster, according to their Y position in the layout so that characters farther up on the layout appear behind characters farther down. This needs to be updated every tick as characters - enemies and Buster alike - move around.

    SET UP

    I created two Families with the same sprite objects to compare instances of the Hunter character against each other as if they were literally two separate objects. Buster and the Hunters will be in front of or behind each other depending on their position on the Y axis. The idea is that the camera is above and slightly behind the characters for that "angled down and forward" perspective...albeit in 2D.

    PROBLEM

    ...The problem is that it's not working (why else would I make this post, right?).

    Basically, what I want to do is what you can see in this image...but reversed. What's in the image is backward from what I want. The problem is that I have tried swapping out the conditions in various arrangements but I cannot get it to work right. Sometimes one instance will work properly but the others will not. In one case, as I moved Buster up and down, the Hunter instances would move in front and to the back of each other for no apparent reason (they're all stationary except for Buster).

    This is my latest event sheet attempt... At one point, I had this working for the Hunter instances but I had forgotten to account for Buster as he moved along. And thus my new problem here.

    Your help would be greatly appreciated. Thank you.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • There's actually a specific action for this which is recommended to be used. From the manual:

    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.

    So it should look like this

    Keep in mind that in your case, you most likely want to sort depending on where the objects feet are (the bottom of the sprite) and not the sprites Y position (which could be in the middle of the sprite) So you can set the variable to self.bboxbottom instead.

  • WackyToaster That seems so obvious now that you present it to me. HA! Go figure...

    I did check the manual to read up on the actual For Each (Ordered) condition but I didn't think to look up anything on Z Ordering.

    THANK YOU! I'll give that a go.

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