-1

私は初めてですがcocos2D、衝突の検出方法に問題があります。衝突CGRectIntersectsRect後にオブジェクトが削除されても、100以上のテキストメッセージを1回表示する必要がありますCCLabelTTFが、複数回追加されます。コードの下

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

   }
}

助けてください。

4

1 に答える 1

0

配列からもスプライトを削除し、if ステートメントにブレークを追加します

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;
}
于 2013-08-12T10:53:50.320 に答える