I am an interactive digital artist at Crispin Porter + Bugosky. Currently creating RIA's and pushing the powers of flash/flex. To view my work or to contact me, please visit my portfolio - http://www.previsualsdesign.com. The dawn of Flash CS3 means the ability to use actionsript 3.0. No longer can you only access the new powers of actionscript with just flex 2. In this tutorial, I am going to discuss one of the main diffrences between Actionscript 3.0 and Actionscript 2.0/1.0, which is the display list, DisplayObjects, and DisplayObjectContainers.First, lets discuss the display list. The display list manages all objects that can be displayed in the flash player and it is going to effect the way you add objects to it. Below is an example of how you created a new object (in this case, a MovieClip) and added it to the stage in AS2.0/1.0, and how it is now done in AS 3.0.
actionscript
1.0/2.0
[as]//main timeline: frame 1
this.createEmptyMovieClip("my_mc", this.getNextHighestDepth());
trace(my_mc);
//outputs: _level0.my_mc[/as]
actionscript 3.0
[as]
//main timeline: frame 1
var myMovieClip:MovieClip = new MovieClip();
this.addChild(myMovieClip);
trace(myMovieClip);
//outputs: [object MovieClip][/as]
The first thing you'll notice is that in AS 3.0, you can now create any object by using the “new” statement. This includes Buttons and TextFields as well. The next thing you'll notice is that you no longer have to give a depth to an object, this is because “depth” no longer exsists in actionscript. Instead when you create an object in AS 3.0, it is created, but not automatily added to the stage. The display list manages all visible objects on the stage, anything that is not apart of the display list is concidered invisible. In order to add the object to the stage, we must use the addChild method of any DisplayObjectContainer (the main timeline or “_root” is a DisplayObjectContainer, I'll discuss this shortly). You may have aslo noticed that in AS 3.0, we did not give our movieClip object an instance name. In oder to do this, we need to use the MovieClip.name property and set the name. So for our above example:
[as]
myMovieClip.name = "my_mc";
[/as]
If your are working mainly with actionscript though, you may not need to set the name property, since when you create the object you give it a variable name which you would use to refrence the object instead.
Ok, let us move on to DisplayObjects and DisplayObjectContainers
The DisplayObject class is the base class for all objects that can be placed on the display list. There are to types: DisplayObject and DisplayObjectContianer. DisplayObjects are objects that can have no children (such as Shape or TextField Objects). So, as you may have assumed, the DisplayObjectContainer is an DisplayObject that can have children, being either displayObjects or other DisplayObjectContainers. DisplayObjectsContainers are what allow you arrange objects on the stage. Lets examine the example below:
[as]//main timeline: frame 1 var myMovieClip:MovieClip = new MovieClip(); var container1:Sprite = new Sprite(); var container2:Sprite = new Sprite(); container1.addChild(myMovieClip); container2.addChild(container1); this.addChild(container2);[/as]
As you
can see, I have created three Objects, one MovieClip and two Sprites
(sprites are new to AS 3.0, they are basicly a lightweight MovieClip
containing no timeline, perfect for containers). I then added them to
one another using the addChild method. Adding MyMovieClip as a child
to container1, container1 as a child of container2, and finally,
contiainer2 as a child of the main timeline or _root. Below is one way we could visualize the display list:
display list
main
timeline ( _root)
container2
(child of _root)
contianer1
(child of container2)
myMovieClip
(child of container3)
The beauty of the display list and its versiatility over depth managment, is that though our MovieClip object is parented (or nested) inside two other objects (three if you count _root), we can still easily move our MovieClip from one parent to another. Below is an example of moving the MovieClip object to the _root:
[as]//main timeline: frame 1
container1.removeChild(myMovieClip);
this.addChild(myMovieClip);
trace(myMovieClip.parent); //outputs: [object MainTimeline][/as]
Thats it! now myMovieClip is a child of the main timeline or _root. To achieve this in AS 1.0/2.0 would be insanly hard and in most cases impossible.