Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

AS3 syntax question

Participant ,
Oct 01, 2013 Oct 01, 2013

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?

TOPICS
ActionScript
614
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guru , Oct 02, 2013 Oct 02, 2013

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();

Translate
Guru ,
Oct 02, 2013 Oct 02, 2013

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();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 02, 2013 Oct 02, 2013
LATEST

Thank you so much. That resolved the problem right away!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 02, 2013 Oct 02, 2013

Here's an Adobe article you should briefly read to understand variable scope to solve any future headaches:

http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f9...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines