views:

397

answers:

1

I'm working with about 20 objects that are moving around in 3D space. Adobe recommends "Using Matrix3D objects for reordering display":

  1. Use the getRelativeMatrix3D() method of the Transform object to get the relative z-axes of the child 3D display objects.

  2. Use the removeChild() method to remove the objects from the display list.

  3. Sort the display objects based on their relative z-axis values.

  4. Use the addChild() method to add the children back to the display list in reverse order.

Great. That's fine if the objects aren't moving. But what if there are animations happening and one object comes in front of another in z-space? The objects are displayed accoring to the position in the display list, not according to their z-order. How can you make objects respect z-order while animating (make object A appear in front of object B if object A's z-value becomes smaller than object B)? Obviously you can't clear the display list during an animation.

A: 

It's practically the same as adobe's docs says... and it works perfectly for moving objects.

The simplest way, so you have some code reference, and given you have all of your 3d objects in an array, would go something like this:

function zSort(objects:Array) {
    objects.sortOn("z", Array.DESCENDING | Array.NUMERIC); // sort the array on the "z" property
    for each(var clip:DisplayObject in objects) { //loop the array and add the childs in the corrected order...
        addChild(clip);
    }
}
Cay
Yes, but when do you execute this code? Every frame? The objects are in motion and could change z-position at any time. When do you do the sorting?
phil
you would need to do this every frame. The items only render to the screen once per frame, regardless of what your other logic may be doing to their state. So putting a version of this code into an onEnterFrame handler would take care of it.
JStriedl
Exactly, you do this each frame... for 20 objects you wont even notice it cpu-wise ;)
Cay
Thanks, that works. I didn't think sorting an array every frame would be processor friendly.
phil