I'm trying to make a game where you move your player around the screen and collect gold bars. I made an array that i'm going to keep all of my gold bars in, and I want to loop through that array and check for a collision between the player and each gold bar. In my code right now I only have one gold bar but later on I plan on making several different levels that use different numbers of gold bars, so that's why I want a mutable array.
In the header file I have this:
Code:
@interface Game1ViewController : UIViewController {
UIImageView *goldBar;
NSMutableArray *goldBars;
}
@property (nonatomic, retain) IBOutlet UIImageView *goldBar;
@property (nonatomic, retain) NSMutableArray *goldBars;
In the implementation file I have this:
Code:
@implementation Game1ViewController
@synthesize goldBar;
@synthesize goldBars;
- (void)viewDidLoad {
[goldBars addObject:goldBar];
}
- (void)gameStatePLayNormal {
// collision between player and each gold bar
for (UIImageView *image in goldBars) {
if (CGRectIntersectsRect(player.frame, image.frame)) {
score++;
[goldBars removeObject:image];
}
}
}
- (void)viewDidUnload {
self.goldBar = nil;
}
- (void)dealloc {
[goldBar release];
[goldBars release];
}
That's all the code relevant to my question. I don't receive any errors when I run it, but it doesn't do anything in the for loop. I think there is something I have to do in the viewDidLoad method to initialize the array but i don't know exactly what I need to do. Is there a better way of doing this or am I on the right track, and what do I need to do to get my for-loop working? Thanks in advance for any help.