#1 (permalink)  
Old 03-10-07, 18:27
Administrator
Living At The FlepStudio!
 
Join Date: Jul 2007
Location: Cesenatico
Posts: 4,917
Rep Power: 6
Flep is on a distinguished road
swapDepths removed !

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 !
__________________
_________________________________________
VIDEO CORSI ACTIONSCRIPT 3.0 creati da FlepStudio
I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread please !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !

Last edited by Flep; 28-08-08 at 05:16.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
sponsor links
Flashmint flash templates FlippingBook-PDF publisher Flash Media Server Hosting
  #2 (permalink)  
Old 17-07-08, 02:34
Junior Member
Settled In
 
Join Date: Jul 2008
Posts: 1
Rep Power: 0
tribaldude is on a distinguished road
re: swapDepths removed !

This is great!!!

Just to add for those searching like I was there is the

swapChildren();
swapChildrenAt();


in the DisplayObjectContainer Class
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-08-08, 19:24
Junior Member
Settled In
 
Join Date: Aug 2008
Posts: 1
Rep Power: 0
monomusica is on a distinguished road
Unhappy re: swapDepths removed !

Thanks Flep!! That's pretty sweet! Although I have a further in depth question about swapDepths in CS3.......I'm trying to figure out how to get whichever button I push in a row of buttons, to display a certain layer/image and depending on which button I push, the layer that the button correspnds to, moves to the very top of all the other layers and the rest of the layers stay at the same level, so basically, I guess I need to take your code and somehow link it to buttons....is there a way to do this?? I'm still pretty new at Flash!
If what I'm describing isn't clear, a good exampe of some real websites which utilize what I described above is car websites such as volkswagen.com and subaru.com where you can pick the car you want and then go on and add paint color, or the wheels and rims and just keep clicking and adding on the layers until you get the model specifications that you want....That's what I'm trying to figure out but cant find enough info on how to do it
Any help would be AWESOME!!
Thanks guys.

-Chris.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
sponsor links
Reply

Bookmarks

Tags
removed, swapdepths

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
loadMovie...removed ! Flep Tutorials 3 07-07-08 21:40
attachMovie...removed ! Flep Tutorials 5 19-06-08 19:39
swapDepths...rimosso Flep Articoli e tutorials 12 19-06-08 18:12
onRelease...removed ! Flep Tutorials 2 20-11-07 10:15
DuplicateMovieclip...removed! Flep Tutorials 0 27-09-07 12:04



Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0
vBulletin Skin developed by: vBStyles.com
FlepStudio 2007-2009