2013-08-12 4 views
-1

새 질문 : CGRectIntersectsRect은 충돌이 발생한 후에도 객체가 제거 되어도 100+ 텍스트가 표시되면 CCLabelTTF 번 표시되지만 문제는 없습니다. 여러 번 추가됩니다. 코드 번호CGRectIntersectsRect 조건이 참으로 여러 번 호출됩니다.

-(void)detectBonousPtCollision 
{ 
    for (CCSprite *sprite in pointsArray) 
    { 
     NSLog(@"tag value %d",sprite.tag); 
     if (CGRectIntersectsRect(playerSprite.boundingBox, sprite.boundingBox)) 
     { 

     [self removeChild:sprite cleanup:YES]; 
     [label setString:[NSString stringWithFormat:@"score %d",totalScore = totalScore+100]]; 

     CCLabelTTF *ptLabel = [CCLabelTTF labelWithString:@"100+" fontName:@"Marker Felt" fontSize:20]; 
     ptLabel.position = ccp(playerSprite.position.x, playerSprite.position.y); 
     ptLabel.tag = 102; 
     [self addChild:ptLabel]; 

     CCSequence *sequence = [CCSequence actions: 
           [CCMoveTo actionWithDuration:3 position:ccp(playerSprite.position.x+10, playerSprite.position.y+10)], 
           [CCCallFunc actionWithTarget:self selector:@selector(afterAnimation:)], 
           nil]; 

     [ptLabel runAction:sequence]; 
     //[[SimpleAudioEngine sharedEngine] playEffect:@"CrashSound.wav"]; 
     } 

    } 
} 

도와주세요.

+2

if 문에서 휴식을 추가 할 수 있습니다. 하지만 pointsArray 배열에서 제거하는 것을 잊지 마십시오. 나는 이것이 당신에게 암시가 될 것이라고 생각합니다. –

+1

빠른 반복과 제거는 잘 어울리지 않습니다. – Sulthan

답변

0

제거 너무 배열에서 스프라이트와 당신은 자체에서 스프라이트를 제거하여

if (CGRectIntersectsRect(playerSprite.boundingBox, sprite.boundingBox)) 
{ 
    [pointsArray removeObject:sprite]; 
    [self removeChild:sprite cleanup:YES]; 
    [label setString:[NSString stringWithFormat:@"score %d",totalScore = totalScore+100]]; 

    CCLabelTTF *ptLabel = [CCLabelTTF labelWithString:@"100+" fontName:@"Marker Felt" fontSize:20]; 
    ptLabel.position = ccp(playerSprite.position.x, playerSprite.position.y); 
    ptLabel.tag = 102; 
    [self addChild:ptLabel]; 

    CCSequence *sequence = [CCSequence actions: 
          [CCMoveTo actionWithDuration:3 position:ccp(playerSprite.position.x+10, playerSprite.position.y+10)], 
          [CCCallFunc actionWithTarget:self selector:@selector(afterAnimation:)], 
          nil]; 

    [ptLabel runAction:sequence]; 
    //[[SimpleAudioEngine sharedEngine] playEffect:@"CrashSound.wav"]; 
    break; 
} 
관련 문제