x


Multidimensional array in Javascript

I'm trying to get a multidimensional array working. I have:

// Global variable grid of Cards (Cards is my own class)
var aGrid : Cards[,];

function Start(){
    // Init aGrid
    for( var i = 0; i < 4; ++i ){
        for( var j = 0; i < 4; ++i ){
            aGrid[ i, j ] = new Card();
        }
    }
}

function accessGrid(){
    // Try to access initialized aGrid
    // This bombs, saying aGrid is a null value
    var card : Card = aGrid[ intX, intY ];
}

When I run the app, the accessing of aGrid fails saying aGrid is null, even though the intX and intY indexing variables are valid (i.e., are values between 0<4).

Didn't I initialize aGrid correctly in the Start() function? Do I need some type of aGrid = new Card[4,4]; statement?

more ▼

asked Mar 01 '12 at 04:28 AM

StevenCalwas gravatar image

StevenCalwas
66 1 1 3

if it is a fixed array give the size of it throught editor!

Mar 01 '12 at 11:08 AM flamy
(comments are locked)
10|3000 characters needed characters left

0 answers: sort oldest

Actually, while writing the original question, I came up with the correct answer. I added the statement:

aGrid = new Card[ 4, 4 ];

to the Start() function and everything works.

more ▼

answered Mar 01 '12 at 09:31 PM

StevenCalwas gravatar image

StevenCalwas
66 1 1 3

Thanks for taking the time to post your answer to this, I am working though the same problem in the Unity book and was looking for a "#pragma strict" way to do this for the old 3.x code.

Mar 21 at 01:54 AM _Gloam

If this is the answer, please accept it so the question shows up as answered :)

Mar 21 at 03:00 AM Benproductions1

I attempted to do that, but says I do not have permission. I am guessing there is some sort of waiting period before I can mark questions answered.

Mar 21 at 03:04 AM _Gloam

@_Gloam Only an Admin/Moderator or the person who asked the question can accept an answer. My comment was towards @StevenCalwas

Mar 21 at 03:10 AM Benproductions1

@Benproductions1 Gotcha, upvoting was giving me the trouble I suppose. Looks like I am short on karma ATM.

Mar 21 at 03:15 AM _Gloam
(comments are locked)
10|3000 characters needed characters left
Be the first one to answer this question
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3442
x11

asked: Mar 01 '12 at 04:28 AM

Seen: 1586 times

Last Updated: Mar 21 at 04:32 AM