(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

AS3

Thanks
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.
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
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?
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.
[...] #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 [...]
I use addChild(theSpriteYouWantOnTop);
If the sprite is already on screen it moves to the top, does not get added again.