AddThis Social Bookmark Button
 attachMovie in Actionscript 3.0



You might have been wondering where attachMovie is gone in Actionscript 3.0, well no need to look for it because it's gone. In Actionscript 3.0 you will have to use another way to attachMovie clips on the stage at runtime, I am going to tell you all about it in that little tutorial.

Tutorial
The default way

When you create a movie clip in an AS3 Flash file and select "export for actionscript you will notice that the Class and Base class are automatically filled up (see picture below), what does that mean in the AS3 world?



Flash automatically fills the Class name field with the name you gave to your movie clip, square in our case. Also since it is a movie clip that we are creating it automatically fills the Base class field with flash.display.MovieClip.

You don't need to worry about the Base class, that means that your movie clip is going to inherit of all the properties and methods assigned to the MovieClip class. In AS2.0 this was happening in the background but you just did not know about it.

More importantly you can also see that the class square has automatically been assigned to the movie clip, this is an important bit, it means that the compiler will be automatically looking for square.as (a physical class that you actually created) in the directory where your Flash file resides, if it can't find it it will create a default one for you. You don't need to create a square class if you have nothing to put in it.

Ok, our movie clip is now sitting in the library, we now need to create the code that will attach it on the stage, this is what we are doing below:

var mySquare:square = new square();
addChild(mySquare);

As simple as that, we create an instance of the square class (that will automatically be created by Flash or that you created yourself) using the new keyword and then we use the magic word addChild with the name of the square instance we just created. Notice that we are not using any linkage id anymore when we use addChild but instead we are using the name of the instance of the class.

That's it if you run your file you will now see that your movie clip is attached to the the top left corner of the stage.

Create your own class

If you want to create a little class for your square, it is easy, got in FILE > NEW and in the window select ACTIONSCRIPT FILE

Now you can create your class, a very simple square class could be like the one below:

package {

import flash.display.MovieClip;

public class square extends MovieClip{

public function square(){
trace("hello I am an instance of the square class");
}
}
}


Save the actionscript file as square.as in the directory where your FLA resides and here you go, Flash will now use that class instead of a default one.

Hopefully this little tutorial will be helpful for you :)



AddThis Social Bookmark Button
If you think this page is providing useful information, don't hesitate to leave a comment.
flashvalley
 
Copyright ©2006-2008 flashvalley.com - All rights reserved