I am extremely new to objective-c and iPhone programming (although, i have a little more background with C#), and I am learning by doing.
I am currently trying to make a mini platform game and instead of checking each platform on its own to see if my player intersects with it, I want to make an array and a for statement which will take care of that. (Correct me if i am wrong but NSMutableArray
seems a lot like the List
feature in C#)
I typed what i think would work but it hasn't, any ideas why? In my @interface
I have:
@interface ViewController : UIViewController
{
NSMutableArray *platforms;
UIImageView *platform1;
UIImageView *platform2;
UIImageView *platform3;
UIImageView *platform4;
UIImageView *platform5;
UIImageView *player;
}
@property (nonatomic) NSInteger GameState;
@property IBOutlet UIImageView *player;
@property IBOutlet UIImageView *platform1;
@property IBOutlet UIImageView *platform2;
@property IBOutlet UIImageView *platform3;
@property IBOutlet UIImageView *platform4;
@property IBOutlet UIImageView *platform5;
And in my @implementation i have:
- (void)viewDidLoad
{
[super viewDidLoad];
[NSTimer scheduledTimerWithTimeInterval:1.0/60 target:self selector:@selector(gameLoop) userInfo:nil repeats:YES];
gravity = CGPointMake(0,0.195);
[platforms addObject:platform1];
[platforms addObject:platform2];
[platforms addObject:platform3];
[platforms addObject:platform4];
[platforms addObject:platform5];
}
- (void)gameLoop
{
playerVelocity = CGPointMake(playerVelocity.x,playerVelocity.y + gravity.y);
player.center = CGPointMake(player.center.x + playerVelocity.x,player.center.y + playerVelocity.y);
for(UIImageView *platform in platforms)
{
if(CGRectIntersectsRect(platform.frame,player.frame))
{
BOOL check = YES; //break point here to check if it reaches this point
}
}
}
Also, when i simply type:
if(CGRectIntersectsRect(platform1.frame,player.frame))
{
BOOL doubleCHECK = YES;
}
It works.