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