attachMovie with ActionScript 3.0
So if you're like me, you've probably been toying around with Macrodobia's Flash 9 AS 3.0 Preview. And if you're like me, you're trying to figure out how you are going to migrate from ActionScript 2.0 to 3.0. There is a migration guide that gives some clues to where things are headed, but to be honest, it isn't much help when it comes to learning how to really make the change.
One of the first problems I ran into with F9 Preview is the lack of attachMovie. If you check out that previously mentioned migration guide, it conveniently tells us that this method has been replaced with addChild. Right. So the question becomes: How do I work with assets in the library? The answer (at least as of yet) is fairly simple, if you're familiar with Object-Oriented Programming (which you should be, considering upcoming changes in 3.0) In fact, you may have used similar methods in your library from Flash 8 or before.
So what do you do with your MovieClip in the library? You assign a class to it. Let me show you how.
For example, if I had a MovieClip named mcMenu in my library, with all of it's children MovieClips and assets, I would right-click on mcMenu and select "Linkage." After that, just like before, we select "Export for ActionScript." Notice that now we don't have the option of choosing a linkage ID (identifier) for this MovieClip. We have to specify which class it's using. So all you have to do is make a simple class in the same directory with that name (mcMenu.as) and add some placeholders for your assets. By placeholders, I mean that if you had "mcButton" in your meMenu MovieClip, in your class you would declare the "public var mcButton:MovieClip." That's it. Now when your movie is compiled, you can add a child of mcMenu to your stage.
Here's how you could do that (this would go on the first frame of your movie):
var myMC:MovieClip = new mcMenu;
this.addChild(myMC);
//now you can access that child mc
myMC.mcButton.width *= 2;
There you go!

6 Comments:
Hi,
what if I try to add MovieClip in a loop? Like:
for(i=0 ,...)
{
var myMC:mcFromLib=new mcFromLib();
myMC.x=i*20;
addChild(myMc);
}
How can I give then a different variable name?
It will still work just fine.
In the case of your loop, the "myMC" variable becomes a temporary variable. You are using it to reference your created mc, then forgetting that mc and moving on to another. One of the ways that I manage the multiple mc creation is by using arrays to store your mc references. For example:
var mcArray:Array = new Array();
for(var i:uint=0; i<10; i++) {
var myMC:MovieClip = new mcFromLib();
myMC.x = i*20;
addChild(myMC);
mcArray.push(myMC);
}
Now you'll have an array with all of your mc's in it.
var mcArray:Array = new Array();
for(var i:uint=0; i<10; i++) {
var myMC:MovieClip = new mcFromLib();
myMC.x = i*20;
// you can assign a prop. "name" for each mc, then u can access then from the name like this:
myMC.name = "a" + i;
addChild(myMC);
mcArray.push(myMC);
}
// when u need to find it back use:
var target:MovieClip = getChildByName("a3");
Question: What if I have the linkage name of the object that I want to create in a string. How can I create a movie of that object type? Thanks!!
-aCC
what if you are using pre-created movieclips, that are already on the stage in the authoring environment. I am working with graphic designers who lay everything out on the stage as usual. I dont see alot of information out there for handling these types of elements.
Adding them to the display list is a bit confusing...they are already visible....
I'm looking for a AS3 guru to go to work in Austin, TX immediately. Do you know anyone who would want to hear about this? If so, please contact ddouglas@novotus.com ASAP. Thanks!
Post a Comment
Links to this post:
Create a Link
<< Home