Copy link to clipboard
Copied
Hi,
Having a syntax problem/question. When I write this in AS3, I get the correct result: 4
var wordArray1:Array = ["test","apple","orange","olive"];
var theLevel:Number = 1;
trace(root["wordArray" + theLevel].length);
but when I write it this way, inside a function, I get an error:
function test():void
{
var wordArray1:Array = ["test","apple","orange","olive"];
var theLevel:Number = 1;
trace(root["wordArray" + theLevel].length);
}
test();
I can't understand what I'm doing wrong? Any ideas?
in the second case wordArrays scope is limited to the function.
so when you try to adress wordArray with root.wordArray, the main timeline doesn`t know about it.
simple fix: declare it outside of the function.
var wordArray1:Array;
function test():void
{
wordArray1= ["test","apple","orange","olive"];
var theLevel:Number = 1;
trace(root["wordArray" + theLevel].length);
}
test();
Copy link to clipboard
Copied
in the second case wordArrays scope is limited to the function.
so when you try to adress wordArray with root.wordArray, the main timeline doesn`t know about it.
simple fix: declare it outside of the function.
var wordArray1:Array;
function test():void
{
wordArray1= ["test","apple","orange","olive"];
var theLevel:Number = 1;
trace(root["wordArray" + theLevel].length);
}
test();
Copy link to clipboard
Copied
Thank you so much. That resolved the problem right away!
Copy link to clipboard
Copied
Here's an Adobe article you should briefly read to understand variable scope to solve any future headaches:
Find more inspiration, events, and resources on the new Adobe Community
Explore Now