0

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.

Can
  • 8,502
  • 48
  • 57
  • possible duplicate of [Having trouble adding objects to NSMutableArray](http://stackoverflow.com/q/851926) [Cannot add items to an NSMutableArray ivar](http://stackoverflow.com/q/7125326), [\[NSMutableArray addObject:\] not affecting count](http://stackoverflow.com/q/3683761), [\[NSMutableArray addObject:\] not working](http://stackoverflow.com/q/1827058) – jscs Jul 11 '13 at 20:09

2 Answers2

2

You failed to allocate your platforms array. All objects in objective-c are pointers, therefore, probably in your viewDidLoad method, you need a line of code like this:

platforms = [[NSMutableArray alloc] init];
Dan F
  • 17,654
  • 5
  • 72
  • 110
0

If you check platforms before the loop, I believe you will find that it is nil. You need to create it before you use it (viewDidLoad is probably the best place, unless you need it before the view is loaded) - and unlike some languages, operating on a null object will silently return 0, not throw an exception.

Kevin
  • 53,822
  • 15
  • 101
  • 132