“Be inspired, enjoy your work, keep learning and never forget to play.” — gskinner

(As2) swapDepths Vs (As3) setChildIndex

Posted: November 6th, 2007 | Author: Ivan Valadares | Filed under: Actionscript2, Actionscript3, Tutorial | 7 Comments »

AS2 was an anarchy language, if you need to put a movieClip visually above all the things in the stage, you can do movieClip.swapDepths(this.getNextHighestDepth()). So it works, but in reality your are swapping your movieClip with a movieClip that didn’t exist, so in AS3 you can’t do that but we have setChildIndex method. You Can do setChildIndex(numChildren-1)

AS2
AS2
AS3
AS3


7 Comments on “(As2) swapDepths Vs (As3) setChildIndex”

  1. 1 Chris said at 5:24 pm on March 28th, 2008:

    Thanks

  2. 2 JesseJimz said at 6:04 am on May 23rd, 2008:

    So basically, in your example, when swapping the child object at index 0 to index 3, the other children are “pushed” down the stacking order? This IS better than having a movie clip (originally at index 3) killed.

  3. 3 Idoru said at 7:06 am on May 23rd, 2008:

    Yep that’s what happens, it is better but it also forces you to delete that displayObject manually, as in AS2 you’d just ‘replace’ it :)

  4. 4 Reid said at 6:49 pm on June 23rd, 2008:

    This seems to be a common approach, but I’m skeptical. What happens in the following scenario? Let’s assume container is an empty DisplayObjectContainer…

    ———————————–
    var s1:Sprite = new Sprite();
    var s2:Sprite = new Sprite();

    container.addChild(s1);
    container.addChildAt(s2, 42);

    container.setChildIndex(s1, container.numChildren – 1);
    ———————————–

    Now, what are the indeces of s1 and s2?

  5. 5 Idoru said at 9:52 am on June 24th, 2008:

    Well it will throw you an error as there’s no index 42.
    if you want you can immediatly change the index of s1 by container.addChildAt(s2,0); which would make s1 top to s2.

  6. 6 From AS2 to AS3 - Where did it go - swapDepths | [mck] said at 8:00 am on August 27th, 2008:

    [...] #2 not possible anymore… if you want a detailed explanation visit dreaming in Flash blog, and read more about swapDepths() in AS3 (it’s explained with a [...]

  7. 7 David J McClelland said at 4:49 pm on February 26th, 2010:

    I use addChild(theSpriteYouWantOnTop);

    If the sprite is already on screen it moves to the top, does not get added again.


Leave a Reply