【问题标题】:CGRectIntersectsRect condition is true calls multiple timesCGRectIntersectsRect 条件为真调用多次
【发布时间】:2013-08-12 10:40:38
【问题描述】:

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

   }
}

请帮忙。

【问题讨论】:

  • 你从自己身上移除了精灵。但是您忘记将其从 pointsArray 数组中删除。我想这可能是对你的暗示。
  • 请注意,快速迭代和删除不能很好地结合在一起。

标签: iphone cocos2d-iphone ccsprite


【解决方案1】:

从你的数组中删除精灵并在你的 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;
}

【讨论】:

    猜你喜欢
    • 2016-11-30
    • 1970-01-01
    • 1970-01-01
    • 2016-01-03
    • 2014-10-21
    • 2020-10-16
    • 2014-11-24
    • 2021-11-26
    • 1970-01-01
    相关资源
    最近更新 更多