There's a really light class called Prototype which is free to download at
http://codext.com/code/9. It does the same thing except with full
constructor support. It also has a really simple/clean API:
mc = createEmptyMovieClip("mc", 1);
Prototype.makeInstanceof(mc, MyMovieClipClass);
thats it! I've loved it ... works on extending other elements too, like
TextFields or _root (I use that one constantly).
Tyler
On 7/3/06, Bernard Visscher <[EMAIL PROTECTED]> wrote:
I've tested the following a while ago and it works perfectly.
It uses the acme ClassUtilities class.
MovieFactory:
import com.acme.ClassUtilities;
class nl.debit.util.MovieFactory {
private static var instance : MovieFactory;
public static function getInstance() : MovieFactory
{
if (instance == null)
instance = new MovieFactory();
return instance;
}
private function MovieFactory()
{
ClassUtilities.registerPackage();
}
public static function
createMC(target:MovieClip,id:String,className:Function):MovieClip
{
if (instance == null)
instance = new MovieFactory();
if(!className.symbolLinked)
{
var classPath:String = ClassUtilities.getPath(className);
className.symbolName = "__Packages." + classPath;
className.symbolLinked =
Object.registerClass(className.symbolName,className);
}
return
target.attachMovie(className.symbolName,id,target.getNextHighestDepth());
}
}
MyClass:
class MyClass extends MovieClip{
public function MyClass() {
beginFill(0x00);
moveTo(0,0);
lineTo(100,0);
lineTo(100,100);
lineTo(0,100);
lineTo(0,0);
endFill();
}
}
Then use in your code:
var mClip:MovieClip = MovieFactory.createMC(_root,"test",MyClass);
Greetz,
Bernard
> -Oorspronkelijk bericht-
> Van: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] Namens
> Costello, Rob R
> Verzonden: maandag 3 juli 2006 17:05
> Aan: flashcoders@chattyfig.figleaf.com
> Onderwerp: [Flashcoders] Re: Programmatically instantiating a
> class thatextends MovieClip
>
> As a variant on the __proto__ method (which I gather is what
> the compiler actually uses behind the scenes anyway) I also
> bundle the code into a static method
>
>
>
>
>
> Class A {
>
> ...
>
>
>
> // dynamicMc (no library symbol) will be added and subclassed
> to mcSubClass
>
> dynamicMc:MovieClip = McSubClass.addInstance(baseMC);
>
> }
>
>
>
>
>
> class McSubClass extends MovieClip{
>
>
>
> function McSubClass {
>
> //empty constructor
>
> }
>
>
>
> function init(){
>
>
>
> //initialize mc - add dynamic graphics etc
>
>
>
> }
>
>
>
> static function addInstance (base_mc) {
>
>
>
> var newMc;
>
> var nd = base_mc.getNextHighestDepth();
>
> newMc= base_mc.createEmptyMovieClip("mcSubClass"+nd,nd);
>
> newMc.__proto__ = new McSubClass ();
>
> newMc.init();
>
> return newMc;
>
> }
>
>
>
>
>
> }
>
>
>
> maybe mc.__proto__ == MyClass.prototype (below) is better
> than my newMc.__proto__ = new McSubClass ()
>
> my method (i picked up on this list) does have the side
> effect that the constructor can't initialize the mc, hence
> the separate init call after the __proto__ / constructor; all
> wrapped in one method so I don't forget
>
> Rob
>
>
>
>
>
> > Subject: Re: [Flashcoders] Programmatically instantiating a
> class that
>
> > extends MovieClip.
>
> > To: "Flashcoders mailing list"
>
> > Message-ID:
>
> > <[EMAIL PROTECTED]>
>
> > Content-Type: text/plain; charset=UTF-8; format=flowed
>
> >
>
> > Hello :)
>
> >
>
> > it's easy, you must use __proto__
>
> >
>
> > AS2 - MyClass extend MovieClip !!!
>
> >
>
> > MyClass extends MovieClip {
>
> >
>
> > // o Constructor
>
> >
>
> > public function MyClass() {
>
> >
>
> > }
>
> >
>
> > }
>
> >
>
> >
>
> >
>
> > var mc = createEmptyMovieClip("myInstance", 1) ;
>
> > mc.__proto__ == MyClass.prototype ;
>
> > MyClass.call(mc) ;
>
> >
>
> > EKA + :)
>
> >
>
> >