Search code examples
actionscript-3constructorparametersauto-generateassets

Passing parameters to constructor for an AS3 auto-created asset class


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.