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

AS3 Multidimensional Array

Explorer ,
Jul 11, 2014 Jul 11, 2014

Hi.

I'm new to AS3. I would like to know how can I create a multidimensional array (eg. 8x8) using for or while. The values in it must be only: red, blue, green, yellow and black.

Thanks.

TOPICS
ActionScript
551
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

LEGEND , Jul 11, 2014 Jul 11, 2014

If you want to pick the colors randomly, place them in an array and randomly pick one of them by using the Math methods...

var colorArray:Array = new Array("red", "blue", etc...)

for(var i:int=0; i<8; i++){

  mArray = new Array();

    for(var k:int=0; k<8; k++){

          mArray.push(colorArray[Math.floor(Math.random()*colorArray.length)]);

    }

}

Translate
LEGEND ,
Jul 11, 2014 Jul 11, 2014

You will need to use two loops if you want a 2 dimensional array, one nested inside the other.  The first loop will create the individual arrays and the second will assign the elements to each.

var mArray:Array = new Array();

for(var i:int=0; i<8; i++){

    mArray = new Array();

    for(var k:int=0; k<8; k++){

          mArray.push(whichever color you intend);

    }

}

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
Explorer ,
Jul 11, 2014 Jul 11, 2014

Thanks. It helped me a lot. I would like to choose the colours randomely.

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 ,
Jul 11, 2014 Jul 11, 2014

If you want to pick the colors randomly, place them in an array and randomly pick one of them by using the Math methods...

var colorArray:Array = new Array("red", "blue", etc...)

for(var i:int=0; i<8; i++){

  mArray = new Array();

    for(var k:int=0; k<8; k++){

          mArray.push(colorArray[Math.floor(Math.random()*colorArray.length)]);

    }

}

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
Explorer ,
Jul 11, 2014 Jul 11, 2014

Thanks a lot. That's exactly what I wanted.

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 ,
Jul 11, 2014 Jul 11, 2014
LATEST

You're welcome

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