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.

8 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 ;)

Wed, September 23, 2009 @ 1:16 AM

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

Sat, March 6, 2010 @ 8:03 AM

3. John wrote:
This helped me tons, thankyou.

Mon, November 22, 2010 @ 6:21 AM

4. Jose wrote:
Yeah man, you made my day! added to favorites =) God bless you and all your family! yeahhhh

Thu, January 27, 2011 @ 3:20 PM

5. Kallisti wrote:
Thank you!

Tue, May 31, 2011 @ 5:19 PM

6. Zech wrote:
Why do you have a $ before the variable names?

Sat, December 10, 2011 @ 12:55 AM

7. sean wrote:
Hi Zech,

I use a $var convention for all function parameter variables, to say anything starting with "$" means "data passed in" - it makes reading code that much easier to understand data flow, especially in large functions.

Tue, December 13, 2011 @ 12:40 PM

8. Heather wrote:
Thank you for posting this! This helped me out in a huge way!! :oD

Tue, December 27, 2011 @ 9:43 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.