2010-12-04 14 views
6

2つのスプライトを次のように検出しようとしています...しかし、ゲームを実行しようとすると、衝突は起こりません。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では悪い習慣とみなされます。 Appleは、内部ライブラリのアンダースコアで変数名を予約しています。インスタンス変数を本当に分類する必要がある場合は、アンダースコアで接尾辞を付けます。