Skip navigation
Currently Being Moderated

Explain me these codes ...

Jul 31, 2012 9:11 AM

private var mineField:Array=new Array();


public function Main() {

// mine field creation

for (var i:uint=0; i<FIELD_H; i++) {

mineField[i]=new Array();

for (var j:uint=0; j<FIELD_W; j++) {



trace("Row "+i+": "+mineField[i]);


trace("The whole mine field: "+mineField);

// end of mine field creation



This is a system of multiple array. I copied it from the book of game design. I would like you to explain :

"Why in the trace ""whole mine field" is 81 zeros?"


Ok, I predict (I understood from the book), that private var mineField array is array of i and j


I also understood that mineField[i] is an array containing only the i from loop ("0,1,2,3,4,5,6,7,8")

... and mineField[i].push(0) changes all i-s in array to 0 ?


Please, explain, it all mixes in my head together

  • Currently Being Moderated
    Jul 31, 2012 9:39 AM   in reply to milanfiala

    Try to use the debugger and watch the variables.


    Take a look at the manual for push. Push does not change it appends a value to an array. Here the Array is created at mineField[i] = new Array() and the zereos are pushed in it some code lines later.

    Mark as:
  • Currently Being Moderated
    Jul 31, 2012 9:44 AM   in reply to milanfiala

    I don't know that trying to explain it will make it clearer to you, but here's a shot at it...


    mineField is being used as a multidimensional array, or in other words, it is an array containing arrays.


    In the loop using " i ", each element of mineField is being assigned a new array in this line:  


          mineField[i]=new Array();


    For the " j " loop, each element of the new array is being assigned a value of 0 in this line.




    The only values being assigned to the arrays is 0, not 0,1,2,3,4,5,6,7,8,9


    The last line is tracing 81 zeroes most likely because FIELD_H and FIELD_W are both equal to 9.  Each element of the mineField is an array containing 9 zeroes, so when you tell it to trace the array it traces each element of that array separate by commas.  Since each element of that array is another array, it traces them separated by commas as well.


    What that last trace is actually tracing is 9 arrays, each containing 9 zeroes.

    Mark as:
  • Currently Being Moderated
    Jul 31, 2012 11:51 AM   in reply to milanfiala

    I presume FIELD_H and FIELD_W are being set up as const variables somewhere else and you're just pasting relevant code. So I'll reply with the same.


    What I think you're looking for is to get 9 rows with 9 columns, each creating a random amount of mines. The value should be 0 for no mine or 1 for a mine. You'd just need to rewrite the push(0) so it generated a 0 or a 1.




    private var mineField:Array = new Array();


    public function Main()


              // mine field creation

              for (var i:uint = 0; i < FIELD_H; i++)


                        mineField[i] = new Array();

                        for (var j:uint = 0; j < FIELD_W; j++)




                        trace("Row " + i + ": " + mineField[i]);


              trace("The whole mine field: " + mineField);

              // end of mine field creation



    Now a number between 0 and 1 will populate each slot. If you want it to be another value range like 0 - 100 then simply change it to: Math.round(Math.random()*100)

    Mark as:
  • Currently Being Moderated
    Jul 31, 2012 12:07 PM   in reply to milanfiala

    mineField[i] = new Array(); // creates a new array inside of the array mineField at index position i


    mineField[i].push(0); // tells the array mineField[i] to push the value "0" into itself (which is an array)


    By doing the above you are ONLY adding 0's to the array.


    Without the loops which might be confusing you, this is really what's happening:


    var mineField:Array = new Array();


    mineField[0] = new Array(0,0,0,0,0,0,0,0,0);

    mineField[1] = new Array(0,0,0,0,0,0,0,0,0);

    mineField[2] = new Array(0,0,0,0,0,0,0,0,0);

    mineField[3] = new Array(0,0,0,0,0,0,0,0,0);

    mineField[4] = new Array(0,0,0,0,0,0,0,0,0);

    mineField[5] = new Array(0,0,0,0,0,0,0,0,0);

    mineField[6] = new Array(0,0,0,0,0,0,0,0,0);

    mineField[7] = new Array(0,0,0,0,0,0,0,0,0);

    mineField[8] = new Array(0,0,0,0,0,0,0,0,0);


    trace("The whole mine field: " + mineField);

    Mark as:
  • Currently Being Moderated
    Jul 31, 2012 12:28 PM   in reply to milanfiala

    Yes. "i" will start at 0 and go to "FIELD_H" which is presumed to be 9. "j" will do the same and go from 0 to "FIELD_W" which is also presumed to be 9.




    mineField[0][0] = 0;

    mineField[0][1] = 0;

    mineField[0][2] = 0;

    mineField[0][3] = 0;

    mineField[0][4] = 0;

    mineField[0][5] = 0;

    mineField[0][6] = 0;

    mineField[0][7] = 0;

    mineField[0][8] = 0; 


    mineField[8][0] = 0;

    mineField[8][1] = 0;

    mineField[8][2] = 0;

    mineField[8][3] = 0;

    mineField[8][4] = 0;

    mineField[8][5] = 0;

    mineField[8][6] = 0;

    mineField[8][7] = 0;

    mineField[8][8] = 0;



    Remember it starts at 0 and goes until it's LESS than 9, which is why it stops at 8. That's 9 entries anyhow, 0,1,2,3,4,5,6,7,8

    Mark as:
  • Currently Being Moderated
    Jul 31, 2012 1:29 PM   in reply to sinious



    does the same as


    mineField[0][ mineField[0].length ] = 0


    as an array is allways zero based, the length of it is allways one number higher than the last index. At least in Flash as3. Other languages can be different.


    The trace command just gives back all the values stored in the 2 dimensional array. When you look at the debugger at the variables you will see how the array is/was build.

    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points