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(0x000000);
                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" <flashcoders@chattyfig.figleaf.com>
> 
> > 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 + :)
> 
> > 
> 
> > 
> 
> > 
> 
> > 
> 
> > 2006/6/29, Scott Hyndman <[EMAIL PROTECTED]>:
> 
> > >
> 
> > > That's exactly what I mean. As a result you can do cool 
> things like
> 
> > > reparenting -- like moving a button from one window to another. It
> 
> > > handles the MovieClip creation itself.
> 
> > >
> 
> > > A code example really isn't too easy, because the framework that
> 
> > > allows this to be possible is quite large. If you were really
> 
> > > interested, you could look at the code. Here's a link:
> 
> > >
> 
> > > http://tinyurl.com/jqtwv
> 
> > >
> 
> > > It's a gigantic class...so it might be difficult to work through. 
> > > The
> 
> > > important method is createMovieClips(), which is called 
> when a view
> 
> > > (the type of object that encapsulates movieclips) moves to a new
> 
> > > superview.
> 
> > >
> 
> > > Scott
> 
> > >
> 
> > > On 29/06/06, Jim Kremens <[EMAIL PROTECTED]> wrote:
> 
> > > > "Why not subclass object instead? Both ActionStep and 
> ASwing work 
> > > > this
> 
> > > > way, then create movieclips on the fly. It's very nice 
> to work with."
> 
> > > >
> 
> > > > So you never really subclass movieclip, you use 
> composition instead.
> 
> > In
> 
> > > > other words, your class has a movieclip, but it isn't a 
> > > > movieclip.,,
> 
> > > >
> 
> > > > Is that what you mean, or am I missing the point?  Can 
> you give a
> 
> > small
> 
> > > code
> 
> > > > example?
> 
> > > >
> 
> > > > Thanks,
> 
> > > >
> 
> > > > Jim Kremens
> 
>  
> 
>  
> 
>  
> 
>  
> 
> ----------- original message ---------------------------- 
> 
>  
> 
> Subject: Re: [Flashcoders] Programmatically instantiating a class that
> 
>       extends     MovieClip.
> 
> To: "Flashcoders mailing list" <flashcoders@chattyfig.figleaf.com>
> 
> 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 + :)
> 
>  
> 
> 
> 
> Important -
> This email and any attachments may be confidential. If 
> received in error, please contact us and delete all copies. 
> Before opening or using attachments check them for viruses 
> and defects. Regardless of any loss, damage or consequence, 
> whether caused by the negligence of the sender or not, 
> resulting directly or indirectly from the use of any attached 
> files our liability is limited to resupplying any affected 
> attachments. Any representations or opinions expressed are 
> those of the individual sender, and not necessarily those of 
> the Department of Education & Training.
> _______________________________________________
> Flashcoders@chattyfig.figleaf.com
> To change your subscription options or search the archive:
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
> 
> Brought to you by Fig Leaf Software
> Premier Authorized Adobe Consulting and Training 
> http://www.figleaf.com http://training.figleaf.com
> 

_______________________________________________
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com

Reply via email to