- Variables declared inside for loops should not be used outside of the scope.
Workaround. If you need, let's say 7 variables why not to store them inside an global array?
private var _variables:Array = []; function processXML(e:Event):void { var str:String; for (var i:int=1; i <= 7; i++) { str = new String(); str = 'str' + (i).toString(); _variables.push(str); trace ('str: ' + i); } } function getVariable(index:int):String { return _variables[index] as String; }
AS3 | for create global variable
-
14-10-2022 - |
Question
How to make FOR to create global variables ?
function processXML(e:Event):void {
for (var i:int=1; i <= 7; i++) {
var str_(i):String = ('str' + (i));
trace ('str: ' + i);
}
}
Output console exmaple:
str: 1
str: 2
str: 3
str: 4
str: 5
str: 6
str: 7
Solution
OTHER TIPS
What do you want to do exactly? I am not sure what you want but I would create somewhere an object(or Array,Dictionary) and in the for loop I append the variables generated.Having all those generated objects inside a object/collection will make it easy to manage them.So try making a class, add a static member that is an Array or Dictionary and add soem static methods for appending new objects(that are generated in the for loop)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow