Cocos2d游戏中的碰撞检测?

6
我正在尝试以以下方式检测两个精灵的碰撞,但当我尝试运行游戏时,没有发生任何碰撞...我可能做错了什么?
- (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 精灵从屏幕顶部动画到底部,monkey 精灵在底部从左到右动画,projectile 穿过 monkey,但 log 没有被调用???

- (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 *)touch withEvent:(UIEvent *)event {

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 *)touch withEvent:(UIEvent *)event {

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

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个回答

25

你应该使用内置的功能:

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

if (CGRectIntersectsRect(projectileRect, targetRects))
{
    NSLog(@"ha ha Collision detected");
}
boundingBox方法考虑了更多因素,例如节点是否相对于其父节点定位。
另外,请注意在Objective-C中使用下划线作为变量前缀被认为是一种不好的实践。苹果公司保留了具有前导下划线的变量名称用于其内部库。如果您确实需要分类实例变量,一种常见的方法是在它们的末尾添加下划线。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接