1

In my code I have written this but it fails to compile:

In Class1.h:

@interface Class1 : CCSprite
{
    NSMutableArray *leafArr[20][20];
}

@property(readwrite, assign) NSMutableArray *leafArr; 
@end

In Class1.m:

@implementation

@synthesize leafArr[20][20];
@end

But this fails to compile, please can you tell me how to make set and set method for a 2D array?

0

2 Answers 2

2

There's no way of creating a 2D array in Obj-C, the only thing you can do is create a normal array, and then add arrays to it.

@interface Class1 : CCSprite
{
    NSMutableArray *leafArr;
}

@property(readwrite, assign) NSMutableArray *leafArr; 
@end

And you add elements with:

[leafArr addObject:mySecondArray];
Sign up to request clarification or add additional context in comments.

1 Comment

Okay thanks, give me a further suggestion then. I need to display some leaves all over the screen in 5 by 10 matrix, as in 5 rows and 10 columns. So how do you suggest I do this? I would of-course make a sprite and add them to the array to keep count. But can you show me how to maintain this array?
1
int str2Darray[9][9] = { 

    {-1, -1, -1, 1, 1, 1, -1, -1, -1}, 
    {-1, -1, -1, 1, 1, 1, -1, -1, -1}, 
    {-1, -1, -1, 1, 1, 1, -1, -1, -1}, 
      {1, 1, 1, 1, 1, 1, 1, 1, 1}, 
      {1, 1, 1, 1, 0, 1, 1, 1, 1}, 
      {1, 1, 1, 1, 1, 1, 1, 1, 1}, 
    {-1, -1, -1, 1, 1, 1, -1, -1, -1}, 
    {-1, -1, -1, 1, 1, 1, -1, -1, -1}, 
    {-1, -1, -1, 1, 1, 1, -1, -1, -1}, 

};

You can define 5 x 10 matrics as above in .m file. Define flag for leaves in 1 2d array & value into another 2d array. check at the time of display according to your requirement.

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.