Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 02-12-2012, 10:31 PM   #1
wu-tang
Senior Member
 
Join Date: Jan 2004
Posts: 279
Default How do you areate new instance() when instance is a variable?

I have a base class I've used on 6 different MC's.

Then when I use these MC's my function runs an if statement to check which one to add.

if(id=design) var obj = new design();
if(id=team) var obj = new team();

I want to make it a bit more dynamic and use the id:String to evaluate which new instance to create.

Anyone know how to do this or even if it's possible?

ActionScript Code:
public function loadChapter(id:String):void {     var obj = new instance_of( id )(); }
wu-tang is offline   Reply With Quote
Old 02-12-2012, 10:41 PM   #2
tadster
tadster
 
tadster's Avatar
 
Join Date: Feb 2009
Location: Texas
Posts: 2,111
Default

Hmmm, you can't do it like that with Classes.
I would suggest something like this:
ActionScript Code:
var classOptions:Object = {"design":Design, "team":Team}; //in that Object Design and Team are the Classes.  public function loadChapter(id:String):void {         var obj = new classOptions[id]();  }
__________________
www.actiontad.com - ActionScript and JavaScript sitting in a tree...
tadster is offline   Reply With Quote
Old 02-12-2012, 10:46 PM   #3
wu-tang
Senior Member
 
Join Date: Jan 2004
Posts: 279
Default

Thanks. I didn't think so : (

It works fine with the if statement for now... It would be handy to eval classes like that. Oh well.
wu-tang is offline   Reply With Quote
Old 02-12-2012, 10:52 PM   #4
henke37
Senior Member
 
henke37's Avatar
 
Join Date: Mar 2009
Location: Sweden
Posts: 10,829
Send a message via Skype™ to henke37
Default

I think you missed the flash.util.getDefinitionByName function, it will let you look up Classes based on their fully qualified names.
__________________
Signature: I wrote a pair of articles about the timeline.
henke37 is offline   Reply With Quote
Old 02-12-2012, 10:56 PM   #5
tadster
tadster
 
tadster's Avatar
 
Join Date: Feb 2009
Location: Texas
Posts: 2,111
Default

What I showed you works, and is more flexible than just a whole bunch of if statements, it's just that you can't reference the Class using just 'this'
you have to put a 'hard' reference to the Class constructor somewhere or use getDefinitionByName.
ActionScript Code:
new this["className"](); //can't do that without first calling getDefinitionByName to get the Class definition. //for example - var class:Class = flash.utils.getDefinitionByName("className"); new class();

ActionScript Code:
var someThing:Array = [1,2,3,4]; var two:Number = this["someThing"][1]; //this will work however, just not with Classes.

In your case however, you also want to limit your need of if statements, so the object with Class names in it is an ok option vs getDefinitionByName.

Edit:
yep, I hit submit for this post, then saw Henkes reply, he is correct of course.
__________________
www.actiontad.com - ActionScript and JavaScript sitting in a tree...
tadster is offline   Reply With Quote
Old 02-12-2012, 11:14 PM   #6
wu-tang
Senior Member
 
Join Date: Jan 2004
Posts: 279
Default

Sweet man thanks. Exactly what I wanted!

It works like too:

ActionScript Code:
var objClass:Class = Class(getDefinitionByName(id)); var instance = new objClass;
wu-tang is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 06:41 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.