HElp with multidimensional custom array


#1

Hi,
How can i declare an array in cocos2d-x of two or more dimension containing an custom data type?

regards,

Rakesh


#2

does CCDictionary meet you demand?


#3

@Rakesh Jha
Why not use c++ multi-dimensional array? refer to http://www.cplusplus.com/doc/tutorial/arrays/
Another choice is changing multi-dimensional to map, and use CCDictionary map structure. For example,

array[0][1][2]

you can change to

map['0-1-2']

use just convert dimension int to a string, but this approach will reduce a little performance.


#4

it does not provide a constructor for v 3.0 and above versions so we can’t use this array[0] format
so what is the proper solution for two dimensional array or vector
please reply fast
thanks!!


#5
MyObject** array = new MyObject*[100];
for(int i = 0; i < 100; i++){
    array[i] = new MyObject[100];
}

That’ll produce 100x100 of class “MyObject”.

Of course you should remember about making memory free after use. CC_SAFE_FREE is easiest solution:

for(int i = 0; i < 100; i++){
    CC_SAFE_FREE(array[i]);
}
CC_SAFE_FREE(array);

If your object is Sprite, Node or any other class extending Ref class you should use Vector class which handles memory for you.