0

Im trying to access an individual tile inside of my 2d array of tiles Ive created my array like so:

private var map:Array = [[25,25]];

ive tried several ways including:

map[1][1] = 1;

all that does is give me this:

ReferenceError: Error #1056: Cannot create property 1 on Number.

ive also tried:

map[[1,1]] = 1;

nothing happends that I can tell

The only way that ive tried so far that gets me a result thats not an error is:

map[1,1] = 1;

The issue here is this selects the whole row. Any help would be apreciated..thanks!

2
  • Besides the fact that this error can never occour with the code you are showing here... you are aware that array indices are zero based? Commented Mar 7, 2015 at 18:08
  • well, I got that error....and im not too certain about your second statement if you mean they start at 0, then im aware... these were just examples of my attempts to get what I want to work Commented Mar 7, 2015 at 18:40

2 Answers 2

1

This is not correct way to create 2D array:

private var map:Array = [[25,25]];

This array contains one array which contains two elements. You can't address to the second element like this:

map[1][1] = 1;

because the second element (array) of map doesn't exist.

You can create 2D array this way:

var map:Array = [];
for (var i:int = 0; i < rows; i++)
{
    map[i] = [];// this line adds new row to the 2D array

    // To fill the array by zeros add next loop
    for (var j:int = 0; j < cols; j++)
    {
        map[i][j] = 0;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

To start, I think that to get the error mentioned in your question, your array should looks like this :

var map:Array = [
    [25, 25],     // map[0] = [25, 25] ( array of two elements )
    35            // map[1] = 35 ( just a simple number )
];

So, if you write :

trace(typeof map[1]);  // gives : number

You will get : number, and that's why you can not right : map[1][1] = value; and it's normal that you got the #1056 error.

Here, I don't know if you meant assign the value 1 to your 2nd 25 of map[0] or you want really add or edit map[1][1], in the 1st case, you can simply write :

map[0][1] = 1;    // gives : map[0] = [25, 1]

In the 2nd case, you can do :

map[1] = [any_other_value, 1];  // gives : map[1] = [any_other_value, 1]

Last remark, forget that you got an error and suppose that your map array was just:

var map:Array = [
    [25, 25]
];

Here, you can not also write map[1][1] = value;, why ? Let's use the same method :

trace(map[1]);   // gives : undefined

So sure you can not add a property to an undefined, that's why when you write :

map[1][1] = value; 

You will get an #1010 error : "undefined has no properties. ".

Of course here, we should firstly create map[1] :

map[1] = [];   // or directly map[1] = [any_other_value, value]

And then :

map[1][1] = value;

Hope that can help.

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.