Collision with CCSprite

Posted by Coder404 on Game Development See other posts from Game Development or by Coder404
Published on 2012-05-19T21:46:12Z Indexed on 2012/06/19 21:25 UTC
Read the original article Hit count: 300

Filed under:
|
|
|

I'm making an iOS app based off the code from here

In the .m file of the tutorial is this:

-(void)update:(ccTime)dt {

  NSMutableArray *projectilesToDelete = [[NSMutableArray alloc] init];
  for (CCSprite *projectile in _projectiles) {
CGRect projectileRect = CGRectMake(
  projectile.position.x - (projectile.contentSize.width/2), 
  projectile.position.y - (projectile.contentSize.height/2), 
  projectile.contentSize.width, 
  projectile.contentSize.height);

NSMutableArray *targetsToDelete = [[NSMutableArray alloc] init];
for (CCSprite *target in _targets) {
  CGRect targetRect = CGRectMake(
    target.position.x - (target.contentSize.width/2), 
    target.position.y - (target.contentSize.height/2), 
    target.contentSize.width, 
    target.contentSize.height);

  if (CGRectIntersectsRect(projectileRect, targetRect)) {
    [targetsToDelete addObject:target];             
  }                     
}

for (CCSprite *target in targetsToDelete) {
  [_targets removeObject:target];
  [self removeChild:target cleanup:YES];                                    
}

if (targetsToDelete.count > 0) {
  [projectilesToDelete addObject:projectile];
}
[targetsToDelete release];
  }

  for (CCSprite *projectile in projectilesToDelete) {
[_projectiles removeObject:projectile];
[self removeChild:projectile cleanup:YES];
  }
  [projectilesToDelete release];
}

I am trying to take away the projectiles and have the app know when the CCSprite "Player" and the targets collide. Could someone help me with this?

Thanks

© Game Development or respective owner

Related posts about ios

Related posts about cocos2d-iphone