mike's Banner
mike

Age/Gender: 24, Male
Job: stupids

hello!

Newgrounds Stats

Sign-Up Date:
2/24/00

Level: 20
Aura: Neutral

Rank: Police Lieutenant
Blams: 1,161
Saves: 811
Rank #: 3,423

Whistle Status: Normal

Exp. Points: 4,264 / 4,440
Exp. Rank #: 5,211
Voting Pow.: 6.20 votes

BBS Posts: 382 (0.11 per day)
Flash Reviews: 44
Music Reviews: 0
Trophies: 3
Stickers: 0

Entry #8

Jump to Entry: [ 135 | 6 | 7 | 8 | 9 | 10 | 111621 ]


mike

AS2 style depth sorting in ActionScript 3

Posted by mike Dec. 19, 2007 @ 2:29 PM EST

Easy depth sorting was one of the nice features of ActionScript 2.0. It was great to throw something onto depth 999999999 and forget about it. Isometric games had it easy -- just set each sprite's depth to its y position (or some function of it), and, woo, perfectly sorted sprites!

Sadly, Adobe strips this away from us in ActionScript 3. Depths are gone. Now your Sprites and MovieClips only get a list of children. The order of this list determines the order in which the children are drawn. You still get functions like swapChildrenAt(i, j) and setChildIndex( child, i ) to change the position of an object in the display list. Unfortunately, you can't supply any arbitrary value for i or j. You can only pass an index from 0 to numChildren-1 -- no more absent-mindedly throwing something to 99999, and no more setting depth simply to an object's y position.

The burden of sorting the display list is now the programmer's. Sure, it's a pain in the ass -- but here's where the magic of object oriented programming comes in! You can make a subclass of Sprite or MovieClip and add your own depth property to it:

class DepthSprite extends Sprite
{
private var _depth:Number;
private var _needsSorting:Boolean = false;

public function get depth():Number { return _depth; }
public function set depth(n:Number):void
{
_depth = n;
if(parent is DepthSprite) DepthSprite(parent)._needsSorting = true;
}
}

Now you can sort the display list based on the depths of the children, using any sorting algorithm you please! In my example, I use bubble sort.

protected function sortDisplayList():void
{
// sort my children based on their depths
// (bubble sort)
var len:uint = numChildren;
for (i=0; i < len-1; i++)
for (j=i+1; j < len; j++)
if ( DepthSprite( getChildAt(i) )._depth > DepthSprite( getChildAt(j) )._depth )
swapChildrenAt( i, j );
}

This homebrew depth system also has some big benefits over the old ActionScript 2 depths. For one, you can use floating point numbers for depths instead of integers. Also, you can safely assign two sprites to the same depth, and there will be no flickering (assuming that your sort is stable).

For the sake of clarity, I made the code as simple as possible. But it's slow -- crank the number of particles up to 200, and it starts to chug. There's a lot of room for improvement. Here are some big optimizations:

1. The example code uses a simple bubble sort. This is bad -- it's a slow and unstable sort. More efficient sorting algorithms, like merge sort, would be better.

2. There are lots of calls to getChildAt() and swapChildrenAt(). This actually causes a lot more overhead than you might think! You could store all the children DepthSprites in your own Array, sort the Array, and then rearrange the display list based on the Array. Even better, use a linked list!

3. There are lots of typecasts to DepthSprite. Not only does this cause a ton of runtime type checks, it assumes that all of the children will be a DepthSprite, which might not be true. This goes along with the above idea -- if you use a strictly typed linked list, you avoid the typecasts and you only work with DepthSprites.

see it in action
source

agnryparticle.gif

Updated: 12/21/07 2:23 PM Log in to comment! | Share this!

The People Have Spoken

18 Comments

Dec. 19, 2007 | 2:35 PM q13qew says:

what can l say nice


Dec. 19, 2007 | 2:49 PM silentkat says:

Sweet.


Dec. 19, 2007 | 7:16 PM Josh-B says:

Maybe you should have them bounce off each other when they collide during the changing of depth.


Dec. 20, 2007 | 12:55 AM BoMToons says:

Even though I complained about it in your last post, it is really cool that you use correct terminology and provide links to the term backgrounds in your posts...I've never taken a CS class, so I find it interesting to look into the theories behind what you're talking about. It amazes me how math can be so useful for logical operations, eg: using logs in the merge sort.

I look forward to future posts.


Dec. 20, 2007 | 6:21 AM Notcrud says:

awesome


Dec. 20, 2007 | 8:33 AM LilFugitive says:

Looks awesome, I should use that for something :)


Dec. 20, 2007 | 8:58 PM DanWandin says:

AS3 scares me :(


Dec. 20, 2007 | 9:38 PM 125Maniac says:

AS3 scares me aswell!


Dec. 21, 2007 | 1:02 AM fetusdoctor says:

Depth sorting still boggles me. I wrote up what I thought was the perfect way to organize and sort movieclips for easy addition and removal, only to have something bizzare screw the whole thing up.

Still, it seems simpler than what you're describing and this review gives me more reason to stick with good ol' Flash 8.


Dec. 24, 2007 | 12:16 PM Darkside7000 says:

Awesome. I have no idea what any of that means.


Dec. 25, 2007 | 1:47 AM Afro-Ninja says:

good stuff man, hopefully this will make the as3 transition much easier should I decide to go that route.

any other AS3 snags like the depth sorting?


Dec. 27, 2007 | 12:49 PM puddinN64 says:

No idea what this means but I like it!


Dec. 31, 2007 | 2:40 AM WolfBlitz2 says:

You're old.....
..........
..........
..........
..........
..........
..........
..........
..........
..........
22 years.....old :D


Jan. 1, 2008 | 9:30 PM Penquin11 says:

no offence but that is actually realy simple to do alls u have to do is make the smilies snaler as u go back...


Jan. 2, 2008 | 1:56 PM aldlv says:

that was cool


Jan. 13, 2008 | 8:10 PM TacoFreak says:

Mike <3


Jan. 27, 2008 | 2:47 PM IammeoramI says:

Fuckin saved. Though algorithims are scary.


Feb. 7, 2008 | 5:18 AM GuyWithHisComp says:

Awesome!
Keep posting. Your posts are the most interesting of all the NG staff. ;)

Jump to Entry: [ 135 | 6 | 7 | 8 | 9 | 10 | 111621 ]