"//set the name of the class to call the create() method on
var classToCall = "MyClass";
_global[classToCall].create();

//change the name of the class to call create() on
classToCall = "MyOtherClass";
_global[classToCall].create();

again
classToCall = "AnotherClass";
_global[classToCall].create();"

I'm not currently on a machine where I can test, but that should work
as long as you
declare the classes somewhere first.  I sometimes have a class that I
use to do just that:

class Dependencies {

  public function Dependencies() {
      //list fully declared class names here
      com.class1;
      com.class2;
      com.class3;
  }
}

var d: Dependencies = new Dependencies();
//even better to make it a singleton....

As long as you've listed the fully declared class names somewhere in
your compiled swf, Flash will be able to provide dynamic access to
them.  If you don't list them, it will not allow you to instantiate
them out of thin air.

So, once you've done the above,

classToCall = class1;
_global[classToCall].create();

should work just fine.

Jim Kremens

On 12/30/05, Rich Rodecker <[EMAIL PROTECTED]> wrote:
> yeah, that works, but you are calling the static create() method through the
> name of the class itself.  Say I have a number of classes (MyClass1,
> MyOtherClass, AnotherClass) and I be able to alter which class to call the
> create method on.  I'm thinking there's got to be a way to do something
> like:
>
> //set the name of the class to call the create() method on
> var classToCall = "MyClass";
> _global[classToCall].create();
>
> //change the name of the class to call create() on
> classToCall = "MyOtherClass";
> _global[classToCall].create();
>
> again
> classToCall = "AnotherClass";
> _global[classToCall].create();
>
>
>
>
> On 12/29/05, JesterXL <[EMAIL PROTECTED]> wrote:
> >
> > Hrm... how about:
> >
> > class com.packageName.MyClass
> > {
> >     public static function create():MyClass
> >     {
> >         var inst:MyClass = new MyClass();
> >         return inst;
> >     }
> >
> >     public function toString():String
> >     {
> >         return "[MyClass]";
> >     }
> > }
> >
> > import com.packageName.MyClass;
> > trace(MyClass.create());
> >
> >
> > ----- Original Message -----
> > From: "Rich Rodecker" <[EMAIL PROTECTED]>
> > To: "Flashcoders mailing list" <flashcoders@chattyfig.figleaf.com>
> > Sent: Thursday, December 29, 2005 11:35 PM
> > Subject: [Flashcoders] Re: calling a static method on dynamically
> > namedclass?
> >
> >
> > hmm i tried storing a reference to the class in a variable, like
> >
> > var myClass = com.packageName.MyClass
> > >
> > > myClass.create()
> >
> >
> >
> > and i got this message:
> >
> > Static members can only be accessed directly through classes
> >
> >
> > there's gotta be a way to pull this off.
> >
> >
> >
> >
> >
> >
> > On 12/29/05, Rich Rodecker <[EMAIL PROTECTED]> wrote:
> > >
> > > I know you can instantiate objects by doing:
> > >
> > > var myObj = new _global[className]();
> > > var className = "TestClass";
> > >
> > > but is it possible to call a static method of a class in a similar way?
> > > Im trying these two ways, and its not working for me:
> > >
> > > _global[iconName].create()
> > > _global[iconName]().create()
> > >
> > >
> > > if I try and do a trace on the class like trace( _global[iconName]) i
> > get
> > > undefined.  The icon classes are subclasses of movieclip so that might
> > > make
> > > a difference.
> > >
> > _______________________________________________
> > Flashcoders mailing list
> > Flashcoders@chattyfig.figleaf.com
> > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
> >
> > _______________________________________________
> > Flashcoders mailing list
> > Flashcoders@chattyfig.figleaf.com
> > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
> >
> _______________________________________________
> Flashcoders mailing list
> Flashcoders@chattyfig.figleaf.com
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>


--
Jim Kremens
_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to