Set Z-Order of Sprites and TiledBackgrounds.

0 favourites
  • 6 posts
From the Asset Store
10 Beautiful Pokemon-like Building Pixel Art Sprite Sets
  • I need help sorting the z-order of Sprites and Tiledbackgrounds together. I can do them separately, but since they are not being ordered together they are not ordering properly. I am using Tiledbackgrounds as walls that the player can walk behind. The width of the walls ranges from 192 to 1792. I would like to keep the walls as Tiledbackgrounds as using objects would increase object count or increase the amount of images in the project. It would be great if the player object z-order was adjusted too, but I am sure I can solve that later.

    **********Edit************

    I figured something out, but AllanR showed me something more efficient using an array. This is as efficient as I can get it.

    dropbox.com/s/1v415k8h85jp2wa/Tutorial.c3p

    I used beta. You may have to update your C3, don't forget to change it back if you don't use beta.

    Tagged:

  • the quickest way I could think of was to throw all the y values in an array (along with the UID and "type") and then sort the array. Then loop through the array picking the appropriate object by UID and moving it to the top. That way it can all be done in one pass.

    I added the player object as well, and call the sort function whenever the player is moving (no need to constantly resort if nothing is changing).

    https://www.rieperts.com/games/forum/Zsort.c3p

  • I updated the link soon before you posted and a few minutes after you posted. I didn't think of using an array. I will probably try to use an array similar to yours. I don't use arrays often so a small bit of your C3P confuses me. I will test them all for best performance though by setting Framerate mode to unlimited ticks. Thank you for the idea.

  • I'm not sure if this is exactly what you're after but see my comment in this thread: construct.net/en/forum/construct-3/how-do-i-8/sort-layer-ordering-top-down-155027

  • the array is two dimensional - each row holds the y coordinate of the object, the UID of the object, and the object type (P for Player, B for Background family, O for Object family).

    when you sort an array by the x axis, it will sort each row by what is in the first column (that is why I put the y coordinate in first).

    at the start of the Sort function I set the array size to (0,3,1). That clears the array, and leaves room for three columns on each row.

    For each object, we push it's y coordinate to the front of the array (which is row 0, column 0). Then we add the UID and type to columns 1 and 2.

    once all the objects are in the array, we sort it by the x axis. That will put the lowest y coordinate at the top, and the highest y coordinate at the bottom of the array - which is how we want the objects arranged on the screen.

    then we read through the array, bringing each object to the top of the layer. So, in the end, we only loop through each family once, and the final array once.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • calminthenight I am not using a tilemap for anything other than the floor in where I want to implement this. My tilemap is always at the bottom Z order.

    AllanR I was confused by the X axis and the P, B, O. I understand X and the P, B, O now. I was thinking X Axis on the layout instead of X axis in the array. P,B, and O is just an identifier used to identify what kind of object it is.

    Right now I am trying to figure out efficiency. I may arrange all objects in an array as you did. I then will only set the Z-order once on start up because all of the objects are stationary. Since the characters will move I will adjust the player and npc Z-Order by comparing Player/NPC Y to the array and then just moving the player/npc above the UID of the object that is below their Y. If I do it this way I can also delete array entries on some objects that don't need Z-order. I will have to figure this out though. It seems it will be more efficient as it only has to compare Y and then adjust Player object based off of UID.

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