Skip to main content
Known Participant
June 30, 2010
Question

Create A variable with a function

  • June 30, 2010
  • 1 reply
  • 396 views

Is it possible to create variables with a function? I don't mean create my own variable in a function but actually have the function create a unique variable name (eg. exam1, exam2 and so on) they would all have the same data type. If so can anyone point me to an in depth resource on how it works. Thanks!

This topic has been closed for replies.

1 reply

June 30, 2010

You can't use strong typing but it's possible with bracket notation like so:

function makeVariables(){

     for(var i:int = 1; i < 5; i++){

          this["exam" + i] = i;

     }

}

That will make variables exam1, exam2, exam3, exam4 - with values 1,2,3,4 in them. You can use a movieClip reference, instead of 'this', to place the variables inside some container.