NSMutableArray *_targets;
NSMutableArray *_projectiles;
_targets = [[NSMutableArray alloc] init];
_projectiles = [[NSMutableArray alloc] init];
[_targets release];
_targets = nil;
[_projectiles release];
_projectiles = nil;
target.tag = 1;
[_targets addObject:target];
projectile.tag = 2;
[_projectiles addObject:projectile];
if (sprite.tag == 1) { // 타겟.
[_targets removeObject:sprite];
} else if (sprite.tag == 2) { // 총알.
[_projectiles removeObject:sprite];
}
- (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];
}
[self schedule:@selector(update:)];
'프로그래밍 > Cocos2D' 카테고리의 다른 글
Cocos2D #1 - Cocos2D 튜터리얼로 간단한 아이폰 게임 만들기 (0) | 2010.07.28 |
---|---|
[MammothHunting] - Cocos2D를 사용한 아이폰 게임 개발 튜토리얼 4 (0) | 2010.07.02 |
[MammothHunting] - Cocos2D를 사용한 아이폰 게임 개발 튜토리얼 2 (0) | 2010.07.02 |
[MammothHunting] - Cocos2D를 사용한 아이폰 게임 개발 튜토리얼 1 (0) | 2010.07.02 |
코코스2D엔진에서 스프라이트 에니메이션을 특정프레임만 재생하고 싶습니다. (2) | 2010.01.20 |