CGRectIntersectsRect条件是多次为true调用

时间:2013-08-12 10:40:38

标签: iphone cocos2d-iphone ccsprite

我是cocos2D的新手,我在检测碰撞方法方面存在问题,即使对象被删除,CGRectIntersectsRect也是如此,在碰撞后需要显示100+ texted {{ 1}}一次,但它会多次添加。在守则之下

CCLabelTTF

请帮忙。

1 个答案:

答案 0 :(得分:0)

从数组中删除精灵,并在if语句中添加break

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;
}