Skip to main content
Inspiring
August 3, 2014
Answered

AS3 Code problem

  • August 3, 2014
  • 2 replies
  • 479 views

I wrote this code:

match = new Array();

matchHoriz = new Array();

row = 0;

while (row < gridRows)

{

      col = 0;

     while (col < gridCols)

     {

          match = getHorizMatch (row, col);

          if (match.length > 1)

          {

               match.unshift(grid[row][col]);

               matchHoriz.push(match);

              // ***1        

          }

          if (match.length == 0)

          {

               col++;

          }

          else

          {

               col = col + match.length;

          }

          match.length = 0;

     }

     row ++;

}

// ***2

If I place trace(matchHoriz) on position // ***1 it shows the correct result (an array with matched objects).

If I place trace(matchHoriz) on position // ***2 it doesn't show anything.

What is wrong about my code???

This topic has been closed for replies.
Correct answer Amy Blankenship

Correct, when you change to concat, you're no longer putting in a reference to the match array. Instead, you're copying its contents, which is what you apparently thought you were doing before .

2 replies

Amy Blankenship
Legend
August 4, 2014

When you clear match, you clear the references to it that you've pushed into matchHoriz.

gibsyAuthor
Inspiring
August 5, 2014

Before I clear the match array, the array is added to the array matchHoriz so the data should be there no matter what I do with match array data. I figured out what was happening: when I add the match array with push, the data in the matchHoriz array doesn't look as I expected. I changed push with concat and everything is going right now.

Thanks.

Amy Blankenship
Amy BlankenshipCorrect answer
Legend
August 5, 2014

Correct, when you change to concat, you're no longer putting in a reference to the match array. Instead, you're copying its contents, which is what you apparently thought you were doing before .

kglad
Community Expert
Community Expert
August 3, 2014

are you improperly setting a while-limit and not completing those loops?

gibsyAuthor
Inspiring
August 4, 2014

The code is finishing as it should be. The while loop finishes, but somewhere the matchHoriz array is losing it's values.

kglad
Community Expert
Community Expert
August 4, 2014

then check getHorizMatch() for any matchHoriz maniuplation