I am writing a game in objective c and I have come across a problem, which involves collision detection and animations. So basically my game consists of the user firing the gun aimed towards enemies. The actions of the enemies moving and the bullets moving are taken care by animation code. Every 1/12 of a second I check to see if any objects (stored in the bullet array) have collided with any of the enemies (stored in the enemy array), and if they do, then I delete both objects. But, When I delete any of these objects in the arrays due to collision, I guess the iphone perceives that the animation of that object is done, and calls the [animationDidStopSelector], which I do not want to happen. If a bullet and an enemy collide, then I want to release both of them, and if they do not, the bullet is to go to the side of the screen and disappear and the enemy UIImageView changes its image. How can I fix this? Like is there a way to detect if an image's animation has been interrupted and then go about it differently?
Thanks a bunch.
Here is my code:
collision detector
Code:
for (int bulletCounter1=[bulletArray count]-1; bulletCounter1>= 0; bulletCounter1--){
for (int enemyCounter=[enemyArray count]-1; enemyCounter>= 0; enemyCounter--){
UIImageView *enemyImg = [enemyArray objectAtIndex:enemyCounter];
UIImageView *bulletImg = [bulletArray objectAtIndex:bulletCounter1];
if( CGRectIntersectsRect(bulletImg.frame, enemyImg.frame)) {
[bulletImg removeFromSuperview];
[bulletArray removeObjectAtIndex:bulletCounter1];
bulletImg = nil;
[enemyImg removeFromSuperview];
[enemyArray removeObjectAtIndex:enemyCounter];
enemyImg = nil;
break;
}
}
}
enemy move animation:
Code:
UIImageView *enemy = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"enemy.png"]];
enemy.frame = CGRectMake(0, 0, 41.5, 81.0);
int enemyx = random() % 480;
enemy.center = CGPointMake(enemyx, -40);
[enemyArray addObject:enemy];
[self.view addSubview:enemy];
[UIView beginAnimations:@"enemy move" context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(objectMoveFinished:)];
[UIView setAnimationDuration:3.0];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
enemy.center = CGPointMake(parax, 200);
[UIView commitAnimations];
[enemy release];
enemy = nil;
code for the firing bullets method is very similar.