Constructor is a method which is called when an object of a class is instanced.
Like in any programming language, in actionscript constructor must have the same name the class has.
Constructor, as a function, may take any number of parameters.
Which is very handy to pass to initialize the objects.
I would recommend using constructor parameters over calling any additional initializing methods.
Here is a good example of using constructors in ActionScript:
var myPoint:Point = new Point(0,0);
And I can give you a lot of such examples.
However, this article is about a particular case, passing parameters to a constructor of your class, which is inherited from (extends) MovieClip.
ActionScript 3, you’re the language I love! You will face no problem developing in AS3, and probably will not think about any difficulties until you will need to do the same in ActionScript2.
Happy ActionScript3 working code:
package
{
public class myTriangle extends MovieClip
{
private var _myName:String;
private var _p1:Point;
private var _p2:Point;
private var _p3:Point;
public function myTriangle(myName:String,
p1:Point,
p2:Point,
p3:Point)
{
_myName = myName;
_p1 = p1;
_p2 = p2;
_p3 = p3;
draw()
}
private function draw()
{
//here we draw our triangle
}
}
}
//using our triangle class
var trian:myTriangle = new myTriangle( "one",
new Point(0,0),
new Point(5,5),
new Point(0,5) );
addChild(trian);
That’s it. New instance of triangle is created and displayed when “trian” object is defined.
You may ask why would you ever need to code AS2 functionality if you have it done in AS3.
I will answer, that there might be some reason. For example when you need to develop a flash component compatible with both ActionScript2 and ActionScript3.
ActionScript2. To create an instance of MovieClip – derived class, you had to add an empty symbol into gallery of fla file, and link it with your class, which extends MovieClip.
Then, in code you would use attachMovie method to actually create a movie clip and cast it to your class.
The following AS2 example will compile, but won’t function as expected.
class myTriangle extends MovieClip
{
private var _myName:String;
private var _p1:Point;
private var _p2:Point;
private var _p3:Point;
public function myTriangle(myName:String,
p1:Point,
p2:Point,
p3:Point)
{
_myName = myName;
_p1 = p1;
_p2 = p2;
_p3 = p3;
draw();
}
private function draw()
{
//here we draw our triangle
}
}
//using our triangle class
var trian:myTriangle = myTriangle( this.attachMovie(
"myTriangleMovie",
"trian",
this.getNextHighestDepth()) );
You see, you have no way to pass parameters to the constructor (“one”,new Point(0,0), new Point(5,5),new Point(0,5))
Here is the solution.
First of all you need to make those parameters to be public in your class:
class myTriangle extends MovieClip
{
public var _myName:String;
public var _p1:Point;
public var _p2:Point;
public var _p3:Point;
Then move all non-assigning constructor code to onLoad() function
private function onLoad()
{
//your class is MovieClip derived, so onLoad will work as expected
draw();
}
And finally create your class instance in the following way:
var trian:myTriangle = myTriangle(this.attachMovie(
"myTriangleMovie",
"trian",
this.getNextHighestDepth(),
{_myName:"one",_p1:new Point(0,0), _p2:new Point(5,5),_p3:new Point(0,5)} ));
That’s a good technique when converting AS3 code to AS2 😉
If you are looking for more skills please visit our book store, where we list a number of highly recommended books
Super-small thing — note spelling of myTrianlge
Great post!
Thanks Dan for that point 😉 it’s been corrected.
Have transcribed your AS2 code exactly into CS3, and when I test my movie – nothing happens. flash.geom.Point is imported in both the movie and .as files. I tried including a trace statment in the onLoad event function, and even that does not execute, as if onLoad simply isn’t being triggered. Any thoughts on what might be failing?
Phil,
onLoad should be called, the reason I think it wouldn’t is incorrect linkage.
Did you link your movie clip to the class? (Rightclick on the movie in gallery, select “Linkage…”, select “Export for ActionScript” check box and put in the class name in ‘Class’ field. Note, class name must not contain class file extension )
Also, you may try simply placing an instance of that linked movie clip onto the stage, test it and see if trace in onLoad function is called.
Paul
Paul – in AS3, if you have an MC either in the library or on the stage, the object instantiation won’t allow you to pass parameters into its constructor. onLoad isn’t available in AS2 either.
i.e. take your myTriangle class, but as a physical MC either in the library or on the stage. Its Base Class would be myTriangle and its library name would be something else (i.e. mcTriangle).
Instantiating it would look like this:
var new _myTriangle:myTriangle = new mcTriangle(…some variables…);
When I do the above, I get the error 1136: Incorrect number of arguments. Expected 0.
This is a common need in MVC (i.e. passing the model to a view’s constructor) and in designing views (designers don’t want to draw with lineTo(), fill(), etc, and I don’t blame them!). Only way around it that I’ve found is to use a setter after the instantiation, before adding it to the DisplayList.
Any thoughts?
CG,
I’m afraid you’re mistaken. The only case when such an error of incorrect number of parameters will appear, when you will not create a class for your MC at the stage or in the library.
When you add linkage to a new movie clip symbol in library of AS3 project without a proper name identifier, but without any corresponding .as file, Flash will popup a warning that the class for this movieclip will be created automatically. This auto-class will have a constructor without parameters.
If you will create a proper .as file with corresponding package and class name, and specify proper parameters in the constructor, the creation of this class will require the parameters.
I have done this in a number of projects, so let me see your sample with an error, so I can figure out the possible problem.
onLoad is available in AS2, you’re mistaken again, onLoad does not work only in AS3.
Great post!
Thanks a lot for the as2 ‘workaround’! I applied it to my own as2 project and it worked.
I found that the parameters you want to set with the attachMovie initObject do not necessarily need to be set to public in my class for this to work. Bit sloppy of as2 maybe, but it eliminates the need for those public variables in my class..
I’m sort of new to AS3 and flash, but not web programming. If I create a movie clip, nav_link_mc, for a navigation link that will be reused in a menu, how exactly do I extend the class to add more variables?
Now I have the nav_link_mc set up to export as class navLink, so I can create links dynamically. Where do I put the code for the navLink class that extends MovieClip?
@Drew
Well, that must be pretty simple.
1) You create a movie clip of you navigation link in the library, putting all the stuff in it (I assume link label, and probably background, and maybe some animations for rollover)
2) You link the movie via Linkage dialog, (rightclick on the item in Library and select “Linkage…”) where you should check “Export for ActionScript” checkmark and put the name of your class in ‘Class’ field. As I understood from your post, you did all the above and put ‘navLink’ as a class name?
3) Create navLink.as file, where define navLink class extending the MovieClip, something like that:
package{
public class navLink extends MovieClip
{
public function navLink(){
trace(“Hurray, constructor was called”)
}
}
4) Now, if you create a new variable, your movieclip will be created. However, don’t forget to call addChild to make it visible.
var navLink1:MovieClip = new navLink();
addChild(navLink1);
I hope it helped 😉
@Drew
Note, all the above is about AS3 not AS2/
Cheers
Hi, thanks for the tip, but I’ve tried this but for some reason custom parameters I pass through seem to have no effect. If I pass _x or _y, it works fine, however when I try one of my own, it comes back undefined
var myDot:Dot = Dot(this.attachMovie(“dot_id”,”myDot”,this.getNextHighestDepth(),
{_place:”Place1″,_loc:”place1.php”,_x:100,_y:100}));
class Dot extends MovieClip {
public var _place:String;
public var _loc:String;
public function Dot(iP:String,iL:String){
_place=iP;
_loc=iL;
}
public function onLoad(){
trace(“working”);
}
public function onPress(){
trace(_place);
}
}
@Martin
I think the reason is the folowing.
Your parameters are set correctly through initialization object, however you do overwrite them with ‘undefined’ values when your constructor is called.
Try making your constructor empty:
public function Dot(){
}
Hope that helped
Yep, that worked a charm!
Hi! Can I call addChild to add an instance to the stage in it’s own constructor method?
In other words… Can you call addChild, somehow, in the triangle’s constructor to add automatically the triangle’s instance to the stage once it’s created?
Thanks for the post and for your answer,
Beto Aveiga
Hi Paul,
thanks for the post…had some as2 code that needed some changes and converting it to as3 was not really an option. The as3 way is a bit more logical and my as2 is weak, so I thought I was close….then I read your post and that worked!! I had to add ‘new’ operator in the class tho…
Dam thats usful, it’s been a few years since I’ve done any AS2 programing and now have met someone who insists on using it for a project. It’s taken me hours to get my head back into AS2 thinking. I was completly stumped on extending MovieClip, thanks a million.