How to call and instantiate a library item using AS3 in Flash CS3
Monday, August 6, 2007 9:54When working with Actionscript 3 in Flash CS3 one of the main things you will need to know is how to create a instance of a movie clip so that you can get it to follow the input instructions you will later add to it. This is a very key attribute that anyone learning AS3 that has got passed learning variables,strings,functions and so on, should really know about.
Most actionscript developers will most likely not put their items on the stage when creating their swf they prefer to leave all their objects in the library and use Actionscript to call them out onto the stage as and when they need them. So in this tutorial we are going to find out how to do this using Flash CS3.
To start off we will create a new fia file and rename it shape however the images may show a different name during this tutorial we still want to call the fla shape.fla just for the sake of being organised better.
Then what we need to do is draw out a square onto the stage or any shape you prefer

press F8 to convert the square to a Movieclip with the following settings
what i have done here is set the name of the square to shape as a Movieclip symbol so it inherits all the features of the MovieClip class then set a class name otherwise known as a instance name.
The class name has to be the same as what you will refer to this object as in Actionscript 3 as does the file name of the class but we will tough on that a little later. I also made sure that it is exported in the first frame

This message is basically telling you that there wasn’t a class called shape (assigned to the MovieClip class) before and that it will create one for your use now

Now because how flash cs3 is set up you link the item for actionscript at the time you convert the symbol so there’s no need to link later as in Flash pro 8

You then need to create a new only Actionscript 3 file so that’s not a fla file its a .as file and name it shape

you would then need to enter in the following code that is also explained to make the MovieClip appear on stage
A package is mainly there to organize the classes just like the literal meaning of a package would be to in case or keep an item safe. you will notice that Actionscript takes a lot of literal meanings int metaphors for its own advantage this is however a good thing for people learning actionscript 3 as it makes it easier to relate to therefor easier to learn Packages are also structured according to the directories
in which they are located.
package {
This is where you are going down the to the bottom of the flash.display folders to find the MovieClip class importing it to make sure you can all its functionality
import flash.display.MovieClip;
This is where you are creating and naming the class (the class name must be the same as the file name eg shape.as). Then you extend class with that of the MovieClips functionality so that the shape inherits it all when set up on stage.
public class shape extends MovieClip {
You then create the constructor function which will instantiate the shape note this must also have the same name as the file name and class name.
public function shape() {
This is the gold first we create a variable that you can name anything you wish set it as a MovieClip then = it to a new MovieClip() each time its called by this or another class then addChild which basically adds a child of the shape from the library onto the stage
var shape:MovieClip = new MovieClip();
addChild(shape);
}
}
}
There we have it please do comment thank you
Similar Posts:
- Imitate a dice roll using Actionscript 3 classes in flash cs3
- Actionscript 3 using flash cs3 Tutorial loading external swfs,pngs,jpgs or bitmaps
- Tutorial Applying filters using Actionscript 3 in Flash CS3
- Tutorial Create a reusable button class in Actionscript 3 using Flash CS3
- Remove Child () tutorial in Flash CS3 using Actionscript 3




ronnie says:
August 6th, 2007 at 12:09 pm
Great tutorial i was looking around quite a bit for one with that much detail it helps to know whats going on with the code thanks.
Billy says:
August 6th, 2007 at 1:09 pm
Good job with the tutorial. Very helpful in the actionscript project I need to put out for a client.
shaz says:
August 6th, 2007 at 3:10 pm
This is great feedback guys thanks im working on more problems that Developers run into with classes that have been driving me mad for some time to but finally worked them out i will be adding to this tute soon.
Thanks for the comments
Daniel says:
August 22nd, 2007 at 11:30 pm
I couldn’t understand some parts of this article call and instantiate a library item using AS3 in Flash CS3 | MirazTutorials.com, but I guess I just need to check some more resources regarding this, because it sounds interesting.
Aaron Hall says:
September 13th, 2007 at 6:48 am
It worked but the blackbox isn’t showing up on runtime. Please email me and I’ll send you the files of what I created. This is very odd (this file) but great tutorial!
Whisper says:
May 26th, 2008 at 10:17 pm
Ah thank you! The books that i have so far has completely differentiate actionscript and flash authoring environment…unless this information is at page 800 of the 1000 pages book. This is very informative.
To Aaron : if you already have a different main class in your document class property of your .fla file, you will have to instantiate this library item. In regards to this tutorial, you would add this code to your main class constructor:
var thisshape:shape;
thisshape = new shape();
addchild(thisshape);
Hope that helps!
And thanks a lot for this tutorial!!
Matt Jukes says:
July 31st, 2008 at 11:57 am
Hi, Great Tutorial, however I have encountered the same problem as Aaron Hall, the Image does not so up on the stage at run time.
Any pointers?
jeff mccown says:
August 17th, 2008 at 9:50 pm
I couldn’t get this to work any help?
I used the the code above