(As2) swapDepths Vs (As3) setChildIndex

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

6 Responses to “(As2) swapDepths Vs (As3) setChildIndex”

  1. Chris Says:

    Thanks

  2. JesseJimz Says:

    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. Idoru Says:

    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. Reid Says:

    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. Idoru Says:

    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. From AS2 to AS3 - Where did it go - swapDepths | [mck] Says:

    [...] #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 [...]

Leave a Reply