dcsimg
A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: create object dinamically

  1. #1
    Junior Member
    Join Date
    Feb 2005
    Posts
    5

    create object dinamically

    Hi !

    Should anyone help me ?
    I would like to create objects from variable classes.
    ie:
    x = new (sClassname) (parameters)
    where sClassname is a string variable containing the class name

    Construct a string "new classone (a,b,c)" and eval() it, does not work.

    Thanks for any idea.

  2. #2
    Novemberain Master
    Join Date
    Sep 2004
    Location
    Void
    Posts
    390
    If I understood you right the only way to do it is to use Object.registerClass() method (or so... I''m not sure of it's name... It was a long time ago when I used it for the last time)


    Never give up...

  3. #3
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    Object.registerClass() only associates a class with a movie clip symbol in the library. It won't generate a class instance for you.

    Also, eval is only used, at least in Flash, to resolve targets (object references) from strings; it does not evaluate code in the way that JavaScript does.

    If you just have a classname as a string, assuming the class is defined in _global, you can create an instance using

    code:
    instance = new _global[class_str](arg1, arg2);



    if the whole thing is a string, you'll probably have to do some fancy string parsing to get that out and use a variation on the method above to get what you need.

  4. #4
    Junior Member
    Join Date
    Feb 2005
    Posts
    5
    Using _global is okay.

    My classes are defined in .as files
    (one file for each class, as AS2 requires)

    How to make it accessible trough _global ?
    ... or it is by default, hmm i will try it.

    thanks

  5. #5
    Senior Member
    Join Date
    May 2001
    Posts
    1,838
    Be careful, you get to "use" or "mention" those classes somewhere in the fla so that class codes get compiled into SWF.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center