我的碰撞检测有问题(我使用的是 cocos2d,而不是 box2D 或 Chipmunk)。基本上,我有Player,它是一个 CCSprite,还有Projectiles,也是 CCSprite。到目前为止,使用 CGRectIntersectRect 一切正常,但在游戏中玩家可以激活超级末日激光,它总是在玩家面前。我包含它的方式是我将它添加为player 的子项,因此我没有更多代码可以处理诸如旋转或移动之类的事情。但我遇到的问题是它无法正确检测与射弹的碰撞,因为激光的位置始终为 0,0,因为它是添加到玩家而不是场景中的。
这是我的代码:
播放器.m
- (bool) activateSuper{
bool activated = NO;
if (powerReady) {
NSLog(@"Super Activated!!!!!");
activated = YES;
_state = pSuper;
//Super sprite which is supposed to collide with projectiles
_sprSuper = [CCSprite spriteWithSpriteFrameName:@"Laser.png"];
_sprSuper.anchorPoint = ccp(0.5, 0.0);
_sprSuper.position = ccp(self.contentSize.width/2, self.contentSize.height);
[self addChild:_sprSuper];
//Delete
int duration = 5;
[self runAction:[CCSequence actionOne:[CCDelayTime actionWithDuration:duration]
two:[CCCallBlock actionWithBlock:^{
[self removeChild:_sprSuper cleanup:YES];
powerReady = NO;
curSuper = 0;
_state = pOK;
}]]];
//Empty super bar
GameScene *g = (GameScene*)[self parent];
[g.gameHUD emptyBar:duration];
}
return activated;
}
在GameScene.m中(用于碰撞检测):
- (void) update:(ccTime)dt{
Projectile *pToRemove = nil;
for (Projectile *p in projectiles){
if (player.state == pSuper && CGRectIntersectsRect(p.boundingBox, player.sprSuper.boundingBox)) {
pToRemove = p;
break;
}
}
if (pToRemove != nil) [self destroyProjectile:pToRemove];
}
你觉得我应该关注什么?是否有检测碰撞的简单方法,或者我应该在场景中添加激光并添加代码以使其与玩家一起移动?
非常感谢您的回答!