Reputation: 2121
As one might expect I'm new to Objective-C and Cocoa2d so this might be painfully obvious but I can't seem to get it to work in a way that makes sense.
What I'm trying to do is to make an array of 10 members (more will be added later) full of either [0,1,2,3] integers, later I want these numbers to be used for a level selection screen where 0 means the level is locked (not completed), and the numbers >0 represent the number of stars (out of three) and mean the level was completed, at the end of a level I want to change the array to reflect that the level was completed. For testing purposes I filled the array with "random integers" (0-3) as such (in LevelSelection.m
):
NSMutableArray *LevelCompArray = [NSMutableArray arrayWithCapacity:10];
for(int i = 0; i<10; i++){
int x= (arc4random() % (4));
[LevelCompArray addObject:[NSNumber numberWithInteger:x]];
}
I have not alloced nor init the Array but it seems to work within the function itself -(id) init
though I believe that is a the root of my problem.
My problem is simple, how do I transfer this array (and other data I will create as my game progresses) from class to class (e.g. from LevelSelection.m
to LevelCompleted.m
) and from function to function (e.g. -(id) init
and -(void) launchLevel
). Do I have a centralized file for data that all required classes can access? If that's the case what's with all the talk of a ".plist"? If not, do I transfer the data through methods to the target class(es)/ method(s)? How do I do that?
Any help is deeply appreciated, transferring me to a tutorial would also be incredibly useful.
Upvotes: 0
Views: 137
Reputation: 1359
One way to do it would be, as you said, have a bunch of global variables in a file that everyone #import
s. You wouldn't use a .plist
file; rather, it would just be a .m
like all your other source code.
You could also pass the information to the constructor of any object that needs it. For example, you could do something like [[LevelSelection alloc] initWithCompletionArray:LevelCompArray]
. Then, to make sure that every object has the right information in said array, use notifications.
-(id) initWithStuff:(Stuff *s) {
if(self = [super init]) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(doStuff:)
name:@"LEVEL_COMPLETED"
object:anObject];
}
And then in doStuff:
(which takes an NSNotification*
), you'd have some logic to handle whatever happened, using the notification's object.
Upvotes: 2
Reputation: 280
you shoukld use a property for the array. like that: first you need to create the array in the secondviewcontroller.h, like this:
NSMutableArray *LevelCompArray;
@property (nonatomic, retain) NSMutableArray *LevelCompArray;
then synthesize it in the secondviewcontroller.m:
@synthesize LevelCompArray;
Then, for example, you want to pass Array when a button in FirstViewController is touched. In its action, you can set it to get the instance of your second view controller, like that:
secondViewController.LevelCompArray = someArray;
actually it is passing data between viewcontroller, for more info check this link: Passing Data between View Controllers
hope this help.
Upvotes: 2