Migrating AS2 to AS3 - Easiest "Bring to Front" / "Stay On Top" for a MovieClip

In the days of AS2, the quick and dirty little line...

   mc.swapDepths(mc.parent.getNextHighestDepth()); 

...would do the trick, and the MC would be on top.

In AS3 there is no "getNextHighestDepth" function.  Instead, there is a display stack with no missing slots.

Thus, this is the easiest way to set a MovieClip (or Sprite or DisplayObject, for that matter) to the foremost depth of it's parent container:

public function bringMCToFront($mc:MovieClip){

    $mc.parent.setChildIndex($mc, $mc.parent.numChildren-1);
}

or, as a dirty 1 liner:

  anyMC.parent.setChildIndex(anyMC, anyMC.parent.numChildren-1);

* remember this only sets $mc to the highest depth within it's parent MC.  If the parent MC is beneath other MC's in the same level, the parent will need to have it's depth set as well.  Same for the parent's parent, and so on...

Cheers.

3 comments (Add your own)

1. sean wrote:
almost forgot to mention... thanks to the complete revamp from as2, with AS3 you can a movie clip's / display object's parent ;)

September 23, 2009 @ 1:16 AM

2. Nitin Pareek wrote:
you dont know man how much you haleped me today. thanks a lot.

March 6, 2010 @ 8:03 AM

3. Chanel Jewelry wrote:
First of all, do not hurry when buying Coach Bags , Footwear is the matter of fashion and style. It is advisable to feel free to walk in any physical shop nearby or visit the online UGG Boots on sale shop, and make sure that you clear all your doubts. You can even refer to magazines and fashion blogs to know what type of ghd Hair Straighteners are in fashion. Normally, girls and women prefer to buy shoes according to the seasons. For instance, women choose strappy MBT Shoes for summer and ankle Chanel Jewelry for the winter. Moreover, colour, heels, style, and price also play an important role in Ray Ban Sunglasses shopping.

April 7, 2010 @ 12:32 AM

Add a New Comment

Enter the code you see below:
code
 

Comment Guidelines: No HTML is allowed. Off-topic or inappropriate comments will be edited or deleted. Thanks.