Draw Order by Depth

JoeRice

  • Posts: 18
Hi everyone,

It's been several years since I used Stencyl but I've entered LudumDare46 and I'm looking for some help!

Is there a simple way to have Stencyl order the drawing based on Y value?

I'm just trying to work a depth system for a side on rpg. I've tried some prefabs from StencylForge but none seem to work (unless I've just not configured them correctly but I don't think that's it).

I considered ditching it but I have big trees and it's ruining the look of the game having the player have to run around the big branches etc.


Any help greatly appreciated.

Thanks

Joe


Luyren

  • *
  • Posts: 2816
From the top of my head, you can in an Update event set the Z-order of your actors to it's Y position + its height (make that a behavior). That might organize all actors in the same layer by their Y bottoms.
My Stencyl resources are available here: https://luyren.itch.io/
Cutscenes, RPG Elements, Particles, Map System and many more.

JoeRice

  • Posts: 18
Ah... that's a new block on me.

Doesn't seem to be having the desired effect though, the result is quite erratic. I notice that an actor's z-order never goes above the number of actors in the layer, despite the block trying to set it to a much higher value. Could that be the issue?

Luyren

  • *
  • Posts: 2816
I didn't know that the Z order had that limit. Another idea would be to check what your player actor is colliding with, and set it's Z order to the collided actor's -1, or switch the Z order of the two actors.
My Stencyl resources are available here: https://luyren.itch.io/
Cutscenes, RPG Elements, Particles, Map System and many more.

havana24

  • *
  • Posts: 508
I think you need to use the z order block in a drawing event in order for It to work.
Try to put z order to y of self for each actor in a drawing event and see if that works.
My Website: www.havana24.net

JoeRice

  • Posts: 18
I think you need to use the z order block in a drawing event in order for It to work.
Try to put z order to y of self for each actor in a drawing event and see if that works.

When I do this, all the Z values are maxed out at the total number of objects in the room. I did get a response from the z-block being in the Update event, just not what was expected - erratic changing of Z positions.

JoeRice

  • Posts: 18
I didn't know that the Z order had that limit. Another idea would be to check what your player actor is colliding with, and set it's Z order to the collided actor's -1, or switch the Z order of the two actors.

Yes it's frustrating.

I've ended up going with a very rough solution.

I've made a behaviour for my character that sets its Z order to 1, then cycles through each actor on screen and checks its Y position. If it's greater, it sets the Z order to 2, if not, 0.

Luckily I only have a handful of objects on screen at once as I suspect this will eat FPS very quickly.

Still very pokey though.

merrak

  • *
  • Posts: 2738
Ah... that's a new block on me.

Doesn't seem to be having the desired effect though, the result is quite erratic. I notice that an actor's z-order never goes above the number of actors in the layer, despite the block trying to set it to a much higher value. Could that be the issue?

If you want to get into the technical details, a "Layer" isn't much different than an "Actor". They're both extensions of the Sprite class. Sprites can be nested into each other. Each sprite has a list of children. In theory, this means you could have "subactors" with some tweaking of the Stencyl engine code. Layers use this functionality, though. Placing an actor into a layer places it into the layer's list of sprites.

The position of the actor in the list of sprites is what 'z-order' refers to.

This (below) is the code snippet I use. The function DrawStackWrapper.actorInFrontOfActor( a, b ) is one I wrote to determine which actor is in front of another. Mine is for isometric perspective. You'd want to replace this function with your own, using the y-value to determine which actor is in front of the other.

Code: [Select]
                var atest:Int = DrawStackWrapper.actorInFrontOfActor( a, b );

                // You'll need to use the code blocks provided in the Flow > Advanced palette to utilize 'swapChildren'
                // a.parent.getChildIndex(a) is the code that the 'z index of actor' block calls
                if ( ( a.parent.getChildIndex( a ) > b.parent.getChildIndex( b ) && atest < 0 ) ||
                     ( a.parent.getChildIndex( a ) < b.parent.getChildIndex( b ) && atest > 0 ) )
                    a.parent.swapChildren( a, b );