PDA

View Full Version : How would you approach this?



GoodnightMG
March 11th, 2009, 04:35 PM
Say I wished to create multiple copies of a variable in a loop

such as:

var pots:String = thisString [i]

and then i wish to reference one of these loop generated variables

Now how would you set it up so you could have all those variables of "pots" had their own names so one could reference say: "pots1" "pots2" or "pots17" ?

This kind of situation would bee seen in a situation where I would be bringing in many array values and assigning certain parts of them to the variables. such as array[i][name]

my first guess would be this, but it is not at all in syntax:


for (var i:int = 0;i<300;i++){

var "pots"+[i]:String = thisString[i][2]

}

any ideas?

creatify
March 11th, 2009, 04:53 PM
you need to either use an object, or use an array, several ways to utilize each. The simplest is probably:

so, instead of the way your loop works, try this:


var allPots:Array = [];
for (var i:int = 0;i<300;i++){
var pots:String = thisString[i][2];
allPots.push(pots);
}
// so, instead of accessing "pot0", "pot1" etc. you would simply use the iterator and access the stored pots as:
trace(allPots[0], allPots[1]);

GoodnightMG
March 11th, 2009, 05:04 PM
That makes sense, but now i have another question for you. What if pots was an object or a movieclip not a string. Now i want to attach to that movieclip/object a few variables that can only be able to be referenced through its respective "pots" variable.

such as

pots[1].pans[1]
pots[3].washer[2]
pots[8].gravy[10]

How would i set up the constructor function to build these "sub" variables for each "pots" object? the sub variable needs to be an object, movieclip or an array.

any takers?