Am I trying to create a game where an obstacle is constantly fired at the user. I have an NSMutableArray so I can access all the obstacles as a group. Here is my code:
CALayer *obstacle = [[[CALayer alloc] init] autorelease];
UIImage *obstacleImage = [UIImage imageNamed:@"Obstacle.png"];
obstacle.contents = (id)obstacleImage.CGImage;
obstacle.bounds = CGRectMake(0, 0, starImage.size.width/2, starImage.size.width/2);
int xPosition = (arc4random()%(360-0))+0;
obstacle.position = CGPointMake(xPosition, 20);
[self.view.layer addSublayer:obstacle];
[self.obstacleArray addObject:obstacle];
My questions is: How would I access the objects in this array? I want to be able to access the latest object so I can animate it. I have looked through the NSMutableArray Class Reference , but still can't find anything. I have tried this:
NSLog(@"%d",[obstacleArray indexOfObject:obstacle]);
But all it returns is: 0. Is there an easy solution to this problem that I'm just not seeing? Thanks in advance for any responses.
Use [obstacleArray lastObject]
or [obstacleArray objectAtIndex:[obstacleArray count]-1]
to get the last Object. You can find that in the NSArray Class Reference. (Since it's the parent class of NSMutableArray)