0

I'm getting crazy with arrays so I need your help:

I created a couple of nested arrays:

private var mainArray:Array=[arrayOne,arrayTwo,arrayThree];
private var arrayOne=[item1,item2,item3];
private var arrayTwo=[item4,item5,item6];
private var arrayThree=[item7,item8,item9];

First, when I try for example: trace(mainArray[2][0]) Flash Builder's output is: undefined or null (instead of item7).

Second, I'm not able to find any row/col position (obviously...because every object seems to be undefined):

private function findPosition(row:int,col:int)
{
    for(var row:int=0;row<=mainArray.length;row++)
    {
      for(var col:int=0;col<=mainArray[row].length;col++
      {
           trace(row,col,mainArray[row][col]);
      }
    }
}

It always throws me some type of error (I tryied many self-made solutions and google searches).

I need to find every row/col position and place artworks according to it, I'm creating a tile map, and this may be the simplest way for me(beginner).

Thanks for every possible solution.

1
  • Just for the sake of terminology, you're referring to 2-dimensional arrays. Commented Jul 23, 2014 at 17:16

2 Answers 2

2

You're trying to populate the mainArray before the other Arrays have been created. Move line 1 down to line 4.

private var arrayOne:Array = [item1,item2,item3];
private var arrayTwo:Array = [item4,item5,item6];
private var arrayThree:Array = [item7,item8,item9];
private var mainArray:Array = [arrayOne,arrayTwo,arrayThree];
Sign up to request clarification or add additional context in comments.

6 Comments

I tryied to move mainArray down, the output is always 'null', but I think the problem was another: My 'items' were Bitmaps. So I change them to String and it seems to work now. I thought arrays could contain bitmaps.
Arrays can contain anything. If you were getting null, you were most likely instantiating your bitmaps incorrectly.
[Embed(source="../folder/image.png")] public static const Image:Class; var tile:Bitmap=new Image() as Bitmap; This is the way I create my instance. But now my problem is that I can't figure out how to show a determined tile according to position.
When do you instantiate your items? Perhaps it's the same issue as above, that you need to create the items before adding them into the Array. I would need to see more of your code.
I used a for loop to find the position and 'trace('tile placed');'. It seems to work as I see 12 'tile placed' in the output (my tiles are 12). I only don't understand how can I assign a different position to every single tile, looping through the array.
|
0

you can create a 2-dimensional array all inline

private var mainArray:Array= [
      [item1,item2,item3],
      [item4,item5,item6],
      [item7,item8,item9]
];

or

private var mainArray:Array = new Array();

then in a method add the sub arrays

mainArray.push([item1,item2,item3]);
mainArray.push([item4,item5,item6]);
mainArray.push([item7,item8,item9]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.