-1

我是新手cocos2D,检测碰撞方法有问题,CGRectIntersectsRect即使物体被移除也是如此,碰撞后需要显示 100+ 发短信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 回答