Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

evaluate variable name in a loop in cs3

Explorer ,
Oct 31, 2007 Oct 31, 2007
I want to generate variable names in a loop and assign a object refe. to it.

ex. public var missing1_mc:MissingGameObjects;
public var missing2_mc:MissingGameObjects;
public var missing3_mc:MissingGameObjects;
public var missing4_mc:MissingGameObjects;

Now in a loop i want to do the following;

var ClassReference:Class ;
var missing_obj:String;

for(var i=0;i<usedNo.length;i++){
missing_obj = main_obj + "_" + usedNo [ i ] + "_x";
ClassReference = getDefinitionByName(missing_obj) as Class;
"missing"+ (i +1) + "_mc" = new ClassReference(); //wrong code
"missing"+ (i +1) + "_mc".setpos(150,100);//wrong code
}

How could i do this. Any help is appreciated.
TOPICS
ActionScript
563
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Nov 01, 2007 Nov 01, 2007
if you're trying to use variable object names and variable classes, you can use the following (assuming everything's defined):

Translate
Community Expert ,
Oct 31, 2007 Oct 31, 2007
are you trying to create several instances of one class or one instance from each of usedNo.length classes?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 31, 2007 Oct 31, 2007
"are you trying to create several instances of one class or one instance from each of usedNo.length classes?" - I am trying to create several instances of subclasses of MissingGameObjects class. Subclass name evaluaion is working if i put them in variables with declared names. Now I am trying to put this new subclass instances to several variables which is already declared by evaluating the variable names. I want to evaluate these variable names i.e.

public var missing1_mc:MissingGameObjects;
public var missing2_mc:MissingGameObjects;
public var missing3_mc:MissingGameObjects;
public var missing4_mc:MissingGameObjects;

in a loop (eg. "missing"+ (i +1) + "_mc" in the loop).

I dont want to use missing1_mc , then missing2_mc and so on..I want to evaluate them by "missing"+ (i +1) + "_mc" in a loop. Thanks in advance.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 01, 2007 Nov 01, 2007
if you're trying to use variable object names and variable classes, you can use the following (assuming everything's defined):

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 01, 2007 Nov 01, 2007
Thanks. This can be a way to do it. Alternate way can be as follows :

var ClassReference:Class;
var missing_obj:String;
var missingObjects:Array = new Array();

for(var i=0;i<usedNo.length;i++)
{
missing_obj = main_obj + "_" + usedNo + "_x";
ClassReference = getDefinitionByName(missing_obj) as Class;
missingObjects
= new ClassReference();
missingObjects .setpos(150,100);
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 01, 2007 Nov 01, 2007
LATEST
you're welcome.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 01, 2007 Nov 01, 2007
Any reason an array of references wouldn't be more ideal?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines