Strange layer behavior


(Joseph Sweeney) #1

Hey there, just a follow up on my earlier post about using layers to render walls based on their rotation angle. I noticed some peculiar behavior when assigning entities layer values based on their position in a list sorted by depth.

I have four squares, each a different color, that are positioned such that they form a box. When I press the arrow keys, the box rotates as you would expect.

Part of making sure the box renders properly is determining each square’s depth by looking at its angle. I use a small sorting class to sort each of the four squares by depth.

To set each square’s render depth, I use a really simple for loop:

for (i = 0; i < walls.length; i++) walls[i].layer = i;

Since the closest square has the highest number, this seems like it would cause each entity to be rendered in the proper order - but it doesn’t. I’ve uploaded a sample of what happens instead.

As you can tell, sometimes a square in the back is rendered over the square in the front. Traces indicate that the walls list is sorted correctly but that the world is not rendering the entities in the proper order.

So, I tried messing with the layer number a little bit. As I understand it, it doesn’t matter what number you pick as your layer - it is the order that counts. So all I did was add 5 to each layer:

for (i = 0; i < walls.length; i++) walls[i].layer = i + 5;

Strangely, as you can see in this example, the squares now render in the same order regardless of position.

Experiementing further, I subtracted 5 from each layer instead:

for (i = 0; i < walls.length; i++) walls[i].layer = i - 5;

And now, somehow, all of the squares render properly.

Any ideas on why this is happening? I don’t know the inner workings of Flash or FlashPunk well enough to determine whether this is a problem with my code, the way FlashPunk cycles through and handles entities, or Flash itself.

I’m thinking about saving this project for Unity, once I learn how to use it.


(Abel Toy) #2

The problem is probably in your code, as FlashPunk layers shouldn’t have any issues.

Is that the for loop you’re actually using? I guess you’re sorting the arrays or something, cause it looks like you’re setting the layer property of each square to their index on the array, and the layer won’t change if the index doesn’t…

Also, the memory usage is jumping a lot!