Ciauz! Here is another article dedicated to the less experts but with so much desire to learn.
After having seen how to use the
ex method attachMovie of the classes in various ways (also seen in the article
DuplicateMovieClip removed ), I have received many messages saying that it was too difficult and that many of you had problems to migrate to Actionscript 3.0.
Follow this article because with few lines of code I will very easily explain how to attach a MovieClip from the library of Flash CS3 to the Stage and how to communicate with it.
Let"s get into it"
I create a FLA and save it as "attach_movie.fla", into which I create a MovieClip kept in library as well as placed on Stage.
To the one in library, I assign the name of "mc_container" and to its instance on Stage, the name of "container_mc".
We will see how to attach a MovieClip placed in library inside that first MovieClip created.
I now create another MovieClip in library named "mc_clip". That MovieClip will be the one used with the method attachMovie.
With Actionscript 2.0, we were assigning a linkage ID to the clip to be attached and were then using the method attachMovie passing it the name of the MovieClip.
With Actionscript 3.0, things have changed a bit but do not be scared as it is easier to do that you could think.
To do it with Flash CS3, we will need to create an AS file associated to the MovieClip we would like to attach. In this example, we will assign to it "mc_clip".
I create an AS file and save it as "Clip.as", implemented the following way:
Code:
package
{
import flash.display.MovieClip;
public class Clip extends MovieClip
{
public function Clip()
{
}
}
}
Back to the FLA, I select with the right click "mc_clip" placed in library and select "linkage" in the menu. In the new opened windows, I select the option "export for Actionscript" and add in the first input text field "Clip", leaving the second one as it is.
Let"s click "ok" to save those options, open next the action panel and write:
Code:
var clip_1_mc:Clip=new Clip();
this.addChild(clip_1_mc);
clip_1_mc.x=100;
clip_1_mc.y=container_mc.y;
var clip_2_mc:Clip=new Clip();
container_mc.addChild(clip_2_mc);
As you can see, we have attached clip_mc from the library. To the left, it has been attached to the Stage and to the right, inside the container_mc already on Stage.
Let"s analyse the code.
I create an instance of the Clip class (Clip.as)
var clip_1_mc:Clip=new Clip();
if we would publish the swf now, the clip that we would like to attach would not be visible as we need the method addChild to add it to the Stage. The Clip class extends the MovieClip and addChild wants as a parameter a MovieClip, we add it to the Stage with "this" being our Stage.
this.addChild(clip_1_mc);
now we can refer to clip_1_mc and interact within all its properties.
I position "clip_1_mc" using its x and y properties
clip_1_mc.x=100;
clip_1_mc.y=container_mc.y;
The following two lines will be to attach "mc_clip" placed in library inside "container_mc" placed on Stage of the FLA. Once again, I create an instance of the Clip class (Clip.as) the following way:
var clip_2_mc:Clip=new Clip();
instead of using the addChild with the Stage, we will do it with "container_mc"
container_mc.addChild(clip_2_mc);
PS: If you do not create Clip.as, Flash will do it for you. But you will not have a AS file to write code that refers at that MovieClip. If so, you have to write code on Timeline by calling clip_2_mc.
Source files: