PDA

View Full Version : default constructors with base classes?



the_lar
November 30th, 2008, 04:54 PM
Hi,

I have a 3 movieClips, each with the same base class, I would like to set up basic positioning up when instantiating the clips:

var Mc1:MovieClip1 = new MovieClip1(x, y);

My base class has a default constructor such as this:



package classes {
import flash.display.MovieClip;
public class baseClass extends MovieClip {
public function baseClass (x:int, y:int){
//Do positioning stuff here
}
}
}
The problem is that I get 1203 errors about an incorrect number of arguments. Is it correct that base classes cannot have any arguments on their default constructors and is there any way around this?

Many thanks!

luhter
November 30th, 2008, 05:07 PM
You're initializing wrong
var Mc1 = new baseClass(x, y); If it extends movie clip it will act as a movie clip

scottc
November 30th, 2008, 05:25 PM
package classes {
import flash.display.MovieClip;
public class baseClass extends MovieClip {
public function baseClass (nx:Number = 0, ny:Number = 0){
x = nx;
y = ny;
}
}
}

var thing:baseClass = new baseClass(123, 150);
var thing2:baseClass = new baseClass(); //will be at 0,0 due to the default nx, ny vaules.
Is how i would do it...

the_lar
November 30th, 2008, 07:25 PM
Thanks for the responses, I am using a base class though.

What I am trying to do is pass arguments to the default constructor of the base class when I instantiate a library item which uses the base class:

var item = new libraryItemWithBaseClass(arg1, arg2);

And in my library, item has a base class as above. I'm getting 1136 error now, incorrect number of arguments

senocular
December 1st, 2008, 12:00 PM
When you associate a class with a library symbol as its base class, Flash automatically generates an additional sub class that acts as the instance class (since, afterall, your class is the base class). That new automatic class uses a standard template that does not include a call to the base class constructor outside of the default, no-argument super(). So if your subclass has constructor parameters, you will need to make the defaultable as scottc demonstrated. To create an instance of the library instance, you have to use its Class class, not the base. This, by default, will not have parameters unless you use your own class for the class, but at that point you wouldn't be messing with the base class.