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

Fixing an array index's out of range error

Contributor ,
Sep 10, 2012 Sep 10, 2012

Hi,

Am stuck in an eLearning project that I am working on and need some help fixing a 2007 error that seems to point to an index for an array being out of range.

Here is the previous posting where there are more details: http://forums.adobe.com/message/4685789#4685789

With much help (thank you once again kglad), the script looks like:

var index:int = 0;var numToDisplay:int = 10;var groupA:Array = ["1","2","3"]var groupB:Array = ["4","5","6"]var groupC:Array = ["7","8","9"]var word_array:Array = [];var i:int;for(i=0;i<3;i++){word_array.push(groupA[groupA.length*Math.random()]);}for(i=0;i<3;i++){word_array.push(groupB[groupB.length*Math.random()]);}for(i=0;i<3;i++){word_array.push(groupC[groupC.length*Math.random()]);}

This is used in another part of the script:

function randomWord() {    if(index<numToDisplay){        wordT.text=word_array[index++];        t.reset();        t.start();    } else {  wordT.text= "";    // display score    }    }

The error message (2007) points to:

wordT.text=word_array[index++];

When I trace (word_array)  I get

,,,,,,

,,,,,,,

,,,,,,,,

Out of range index for this array?

How can I fix this out of range index?

Any help appreciated.

TOPICS
ActionScript
2.1K
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 ,
Sep 10, 2012 Sep 10, 2012

Since there appear to be only 9 elements getting added to the array, and your numDisplay is set to be 10, your condition ( if(index<numToDisplay){ ) allows index++ to get to 10, which will be out of range for your array.  See if changing the value of numDisplay to 9 solves the problem.

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
Contributor ,
Sep 10, 2012 Sep 10, 2012

Thanks, Ned for this suggestion.

Changing to 9 does not prevent the error.

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
Community Expert ,
Sep 10, 2012 Sep 10, 2012
LATEST

(that's my fault.  i forgot to include Math.floor to adjust those random numbers to ints.)

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