What is the best way to ysort in this particular case?

Not favoritedFavorited Favorited 0 favourites
  • 5 posts
From the Asset Store
Best car suspension with spring effect and very cool terrain generation.
  • So basically, this is just an example situation, though a real one I'm currently working with. How would I ysort a large object that repeats, for example, a fence, when working also with other object types? So I know how to ysort sprites, using the sorting method. But if I were to have place a ton of sprites on a large map for the fence, that seems..impractical?

    Solution 1: Use a tiled bg object and just stretch the fence. That gets the object in the layout, effectively, but how would I ysort it since I cannot add it the family for sprites to be ysorted? How would I effectivey ysort both the sprites and the tiled bgs?

    Solution 2: Use a tilemap. Problem would be same as above.

    Solution 3: Manually place all the sprites for the fence. Very time consuming, but would work.

    Tagged:

  • There are actions to change the zorder of objects. So even though you can’t sort by y with one action like you can with sprites, there are other ways.

    One way could be two use an array to store the y and uid of all the objects you want to sort. Then you’d sort the array and loop over it and pick the objects by uid and move them to front.

    Another way could be to zsort all the sprites, then loop over the tiledbg objects by y and pick the sprite with the lowest y greater than the tiledbg y. Then you’d set the zorder to be behind the sprite.

    There may be other schemes you could try too.

  • Thanks, your first suggestion I implemented and it works perfectly.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I'd argue the easiest way would be to use javascript since javascript doesn't care about what instance types you throw at it. I'd recommend doing it this way too because the "sort z order" action is specifically designed for this purpose.

    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.

    And the code part is relatively easy to understand.

    const instances = [...runtime.objects.Sprite.getAllInstances(), ...runtime.objects.Fence.getAllInstances(), ...runtime.objects.[name of object or family].getAllInstances()];
    runtime.sortZOrder(instances, (a, b) => a.y - b.y);
    

    Now all you have to do is add all the objects you intend to sort by their Y position in the layout.

  • Thanks, I'll try this too. Should be relatively straightforward since I do know javascript pretty well. I just don't know the API yet for Construct, haven't attempted to learn it yet.

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