The management of depths in Flash CS3 with Actionscript 3.0 has dramatically changed.
With Actionscript 2.0 the swapDepths and getDepth methods seemed simpler to understand, but the Flash Player was often confused by scripts with complicated depths management.
Now with Actionscript 3.0 is the DisplayObject to manage the added child objects depths.
I"d like to add that depths management in Flash CS3 has been nicely optimised and bettered.
Let"s make a concrete example"
I create an FLA called " swap.fla " .
I create the Document Class, an AS file saved as " Swap.as " .
Let"s look at it:
Code:
package
{
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class Swap extends MovieClip
{
private const numSprites:Number=13;
private const higher_depth:Number=numSprites+1;
private var counter:int=-1;
private var clips_array:Array;
private var timer:Timer;
public function Swap()
{
init();
createClips();
startTimer();
}
private function init():void
{
stage.frameRate=31;
clips_array=new Array();
}
private function createClips():void
{
for(var i:int=0;i < numSprites;i++)
{
var clip:MovieClip=new MovieClip();
clip.graphics.beginFill(getRandomColor(),1);
clip.graphics.drawCircle(35+40*i,50,25);
addChild(clip);
clips_array.push(clip);
}
}
private function getRandomColor():Number
{
var red:Number=Math.floor(Math.random()*256);
var green:Number=Math.floor(Math.random()*256);
var blue:Number=Math.floor(Math.random()*256);
var color:Number=Number('0x'+red.toString(16)+green.toString(16)+blue.toString(16));
return(color);
}
private function startTimer():void
{
timer=new Timer(500,0);
timer.addEventListener(TimerEvent.TIMER,scambia);
timer.start();
}
private function scambia(t:TimerEvent):void
{
counter++;
if(counter > clips_array.length-1)
counter=0;
if(counter < clips_array.length-1)
setChildIndex(clips_array[counter],getChildIndex(clips_array[counter+1]));
if(counter==clips_array.length-1)
setChildIndex(clips_array[counter],getChildIndex(clips_array[counter-1]));
}
}
}
Here is the result:
Let"s analyse the code.
The method we"re interested in, where the depth swap happens in the Swap class written by me, is the " scambia " method.
After having created the clips and inserted them in an array, I set a timer to call the scambia method every half a second, which...
increases the value of the variable called counter, used as the index of the array to recall the clip I want
counter++;
now, theoretically, I could swap the levels ( depths ) of the clip with index equal to counter with the levels of the clip with index equal to counter+1, but we"d encounter problems when the counter reaches the last clip (array length minus 1), so we have to implement conditional logic to deal with this, but what we"re interested in is:
setChildIndex, this method would have been like a hypothetical setDepth in Actionscript 2.0 (non existent as in AS2 you couldn"t directly swap a clip"s depth and you needed to swap it with another clip). Instead, setChildIndex of Actionscript 3.0 gives the chance to set the depth of a clip simply passing to it the clip and the value of the depth I want the clip to move to.
How do I know what depth value to pass in "! I obtain it with getChildIndex which gives me the value of the depth of the clip I"m passing in.
That"s the reason for the existence of the following lines:
setChildIndex(clips_array[counter],getChildIndex(clips_array[counter+1]));
setChildIndex(clips_array[counter],getChildIndex(clips_array[counter-1]));
Stay tuned !
Bookmarks