Skip to main content
Participant
June 9, 2011
Question

Can you iteratively create variables?

  • June 9, 2011
  • 1 reply
  • 451 views

Can you use a for(x:int; x < 100; x++) { Var varname + x: vat type }  To create one hundred unique variables  Basically I need to make 100 variables and am wonder how you create 100 vats with out resorting to Declaring them all by hand.   Thanks, Andrew

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
June 9, 2011

yes, use array notation:

for(x:int=0; x < 100; x++) {

this["somestring"+x] = whatever(x);

}

aljones15Author
Participant
June 9, 2011

Thanks for the answer! But I don't think that quite answers it. I need 100 different variables, not setting an array. Also don't you have to declare the variable type?I will try this when I get home. I think I might see how it willwork. Sorry, I'm a little new.

aljones15Author
Participant
June 9, 2011

Yeah, on review I have no idea how this works. You appear to adding together a string and x which is good to know, but I don't get how that becomes a unique variable.

also got this error:

Error: The this keyword can not be used in static methods. It can only be used in instance methods, function closures, and global code.

ok appear to have it working like this:

for (var count:int=0;count < 100;count++) {

this["wavey"+count] = new B2FlxSprite(count, 100, 2, 2, _world);

}

does not work in my app, but does appear to create the vars.

Thanks!

Questions has been answered! Thanks!