Passing parameters to constructor for an AS3 auto-created asset class
-
22-08-2019 - |
Question
I'm creating a MovieClip subclass (let's call it MyClip
) that I want to use for several library assets. I'll be instantiating these movie clips from ActionScript code. MyClip
has a constructor parameter that allows it to set the initial values of certain properties.
Since I want to use it for multiple library assets, the logical way to do it seems to specify it in the "Base Class" text box in the "Symbol Properties" dialog. The problem is that the auto-generated subclasses don't have the constructor with the parameter. Instead, Flash tries to generate them with a default constructor only, which also fails because MyClip
doesn't have a default constructor.
Is there any way around this, apart from deferring property initialization to a normal method?
Edit: I haven't been clear enough, I'll try to clarify here. If this is the MyClip
class:
public class MyClip extends MovieClip
{
private var someValue : Number;
public function MyClip(someValue : Number)
{
this.someValue = someValue;
}
}
and I specified MyClip
as base class for symbol MyClipA in the library, I would ideally like to be able to do clip = new MyClipA(17);
without having to write the MyClipA
class myself.
Solution
I'm assuming the reason you need to create MyClipA and MYClipB, etc classes is to attach different art to objects that just have the behaviors of a MyClip object?
Maybe you could approach it differently, make your base class - MyClip as you have it your example, accept 2 aruguments, one of which is a reference to the art:
public class MyClip extends MovieClip
{
public var art:ArtBaseClass
private var someValue : Number;
public function MyClip(someValue : Number, artClass:ArtBaseClass)
{
this.someValue = someValue;
this.art = new artClass();
this.addChild(art);
}
}
And you could define whatever else you need in a ArtBaseClass that is applied to each of these art library objects.
OTHER TIPS
I don't think it can really be done. But if it can be done, it will properly be with the prototype. Perhaps you can change the contructor of each of the default classes.
Try specifying default arguments for the constructor (null
as a special value, perhaps?). Then add a bit of error checking at the beginning of the constructor that sets sensible defaults for your MovieClip. It's really hacky, but as far as I know there's not a better way to do it using the drag & drop animating UI (you can only do it through code).
If ran into this as well. The proper way to solve it is to create classes for MyClipA and MyClipB that inherit from MyClip. This can be cumbersome if there are lot of classes that are based on MyClip. Then again, if that is the case you might want to start think about using composition instead of inheritance anyway.