2010-12-04 139 views
6

我想以下列方式檢測碰撞兩個精靈 ......但是當我嘗試運行遊戲時,沒有碰撞發生......我可能做錯了什麼?Cocos2d遊戲中的碰撞檢測?

- (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"); 
     }      

}

精靈從屏幕頂部至底部,並猴子精靈從底部從左到右動畫動畫彈丸穿過猴子但日誌不被稱爲?

- (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(@"@@@@@@@@@@@@@@@@@@@@@@@@@@@@");    
} 

}

- (無效)spriteMoveFinished:(ID)發送方{

//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]; 
} 

}

- (無效)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; 

}

- (無效)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; 
     } 

    } 

} 

回答

25

您應該使用內置功能:

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

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

boundingBox的方法需要一對夫婦更多的東西考慮進去,例如,如果節點相對於其父的位置。

另請注意,帶有下劃線的前綴變量在Objective-C中被認爲是不好的做法。蘋果會爲其內部庫保留具有前導下劃線的變量名稱。如果您確實需要對實例變量進行分類,則常用的方法是將它們加下劃線。