Cocos2d游戏中的碰撞检测?

时间:2010-12-04 10:04:24

标签: iphone ios4 cocos2d-iphone

我试图通过以下方式检测两个精灵碰撞 ...但是当我尝试运行游戏时没有发生碰撞....我可能做错了吗?

- (void)update:(ccTime)dt {



    CGRect projectileRect = CGRectMake(projectile.position.x - (projectile.contentSize.width/2), 
                                       projectile.position.y - (projectile.contentSize.height/2), 
                                       projectile.contentSize.width, 
                                       projectile.contentSize.height);

    //CGRectMake(0,220,320,50);
    CGRect targetRects =  CGRectMake(_monkey.position.x - (_monkey.contentSize.width/2), 
                                 _monkey.position.y - (_monkey.contentSize.height/2), 
                                 _monkey.contentSize.width, 
                                 _monkey.contentSize.height);

        if (CGRectIntersectsRect(projectileRect, targetRects)) {
                    NSLog(@"ha ha Collision detected"); 
        }                       

}

projectile 精灵从屏幕顶部到底部动画,猴子精灵从底部向左动画,弹丸穿过猴子,但是日志确实如此不被称为???

- (void)update:(ccTime)dt {

CGRect projectileRect = [projectile boundingBox];
CGRect targetRects = [_monkey boundingBox];

if (CGRectIntersectsRect(projectileRect, targetRects))
{
    NSLog(@"ha ha Collision detected");
}

CGRect projectileRects = CGRectMake(projectile.position.x - (projectile.contentSize.width/2), 
                                   projectile.position.y - (projectile.contentSize.height/2), 
                                   projectile.contentSize.width, 
                                  projectile.contentSize.height);
CGRect targetRect = CGRectMake(_monkey.position.x - (_monkey.contentSize.width/2), 
                               _monkey.position.y - (_monkey.contentSize.height/2), 
                               _monkey.contentSize.width, 
                              _monkey.contentSize.height);
if (CGRectIntersectsRect(projectileRects, targetRect)) {
    NSLog(@"@@@@@@@@@@@@@@@@@@@@@@@@@@@@");             
}

}

- (void)spriteMoveFinished:(id)sender {

//NSLog(@"spriteMoveFinished");
CCSprite *sprite = (CCSprite *)sender;
[self removeChild:sprite cleanup:YES];

if (sprite.tag == 1) { 
    [_targets removeObject:sprite];



} else if (sprite.tag == 2) { 
    [_projectiles removeObject:sprite];
}

}

- (void)addTarget {

projectile = [CCSprite spriteWithFile:@"egg.png" rect:CGRectMake(0, 0, 10, 10)];
projectile.position = ccp(_bear.position.x,_bear.position.y-20);
projectile.tag=2;
[self addChild:projectile];

CGPoint realDest = ccp(_bear.position.x, _bear.position.y - 380);

int minDuration = 2.0;
int maxDuration = 4.0;
int rangeDuration = maxDuration - minDuration;
int actualDuration = (arc4random() % rangeDuration) + minDuration;

// Move projectile to actual endpoint
[projectile runAction:[CCSequence actions:
                       [CCMoveTo actionWithDuration:actualDuration position:realDest],
                       [CCCallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished:)],
                       nil]];

// Add to projectiles array
projectile.tag = 2;
[_projectiles addObject:projectile];

}

-(void) registerWithTouchDispatcher
{
    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}

- (BOOL)ccTouchBegan :( UITouch *)触及withEvent:(UIEvent *)事件{

CGPoint touchLocation = [self convertTouchToNodeSpace:touch];


if(CGRectContainsPoint(CGRectMake(0,0,320,50),touchLocation)) 
{
    if (![_walkMonkey isDone]) {
        [_monkey runAction:_walkMonkey];
    }


}
else {

}

return YES;

}

- (void)ccTouchEnded:(UITouch *)触及withEvent:(UIEvent *)事件{

CGPoint touchLocation = [self convertTouchToNodeSpace:touch];

if(CGRectContainsPoint(CGRectMake(0,0,320,50),touchLocation)) 
{

    [_monkey stopAction:_walkMonkey];

}

}

- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {       

    CGPoint touchLocation = [self convertTouchToNodeSpace:touch];

    CGPoint oldTouchLocation = [touch previousLocationInView:touch.view];
    oldTouchLocation = [[CCDirector sharedDirector] convertToGL:oldTouchLocation];
    oldTouchLocation = [self convertToNodeSpace:oldTouchLocation];

    CGPoint translation = ccpSub(touchLocation, oldTouchLocation);    

    if (translation.x > 3) {

        _monkey.flipX=YES;
    }
    else if (translation.x < -3){
        _monkey.flipX=NO;

    }

    if(CGRectContainsPoint(CGRectMake(40,0,240,50),touchLocation)) 
    {

        CGPoint newPos = ccpAdd(translation,_monkey.position);
        if(newPos.x >= 320 || newPos.x <= 20)
        {
            NSLog(@"monkey not walking");
        }
        else {
            newPos.y = 100;
            _monkey.position = newPos;
        }

    }

}

1 个答案:

答案 0 :(得分:25)

您应该使用内置功能:

CGRect projectileRect = [projectile boundingBox];
CGRect targetRects = [_monkey boundingBox];

if (CGRectIntersectsRect(projectileRect, targetRects))
{
    NSLog(@"ha ha Collision detected");
}

boundingBox方法需要考虑更多的事情,例如,如果节点相对于其父节点定位。

另请注意,使用下划线为变量添加前缀在Objective-C中被视为不良做法。 Apple为其内部库保留带有前导下划线的变量名称。如果你真的需要对实例变量进行分类,一种常见的方法是用下划线对它们进行后缀。