0

我有一个碰撞检测,它工作正常,问题是,每当我bird的“”击中“ cloud”,云消散,我得到一些分数。应该发生的“ sol”也是如此,但云却没有。

这怎么能改变?我尝试了很多,但似乎可以解决。

碰撞代码

- (void)update:(ccTime)dt {
    bird.position = ccpAdd(bird.position, skyVelocity);

    NSMutableArray *projectilesToDelete = [[NSMutableArray alloc] init];
    for (CCSprite *bird in _projectiles) {
        bird.anchorPoint = ccp(0, 0);  
        CGRect absoluteBox = CGRectMake(bird.position.x, bird.position.y, [bird boundingBox].size.width, [bird boundingBox].size.height);

        NSMutableArray *targetsToDelete = [[NSMutableArray alloc] init];
        for (CCSprite *cloudSprite in _targets) {
            cloudSprite.anchorPoint = ccp(0, 0); 
            CGRect absoluteBox = CGRectMake(cloudSprite.position.x, cloudSprite.position.y, [cloudSprite boundingBox].size.width, [cloudSprite boundingBox].size.height);


            if (CGRectIntersectsRect([bird boundingBox], [cloudSprite boundingBox])) {
                [targetsToDelete addObject:cloudSprite];

            }                       
        }

        for (CCSprite *solSprite in _targets) {
            solSprite.anchorPoint = ccp(0, 0); 
            CGRect absoluteBox = CGRectMake(solSprite.position.x, solSprite.position.y, [solSprite boundingBox].size.width, [solSprite boundingBox].size.height);


            if (CGRectIntersectsRect([bird boundingBox], [solSprite boundingBox])) {
                [targetsToDelete addObject:solSprite];      
                score += 50/2;
                [scoreLabel setString:[NSString stringWithFormat:@"%d", score]];
            }                       
        }



        // NÅR SKYEN BLIVER RAMT AF FUGLEN
        for (CCSprite *cloudSprite in targetsToDelete) {
            //[_targets removeObject:cloudSprite];
            //[self removeChild:cloudSprite cleanup:YES];       
        }

        // NÅR SOLEN BLIVER RAMT AF FUGLEN
        for (CCSprite *solSprite in targetsToDelete) {
            [_targets removeObject:solSprite];
            [self removeChild:solSprite cleanup:YES];   


        }

        if (targetsToDelete.count > 0) {
            [projectilesToDelete addObject:bird];
        }
        [targetsToDelete release];
    }

    // NÅR FUGLEN BLIVER RAMT AF ALT ANDET
    for (CCSprite *bird in projectilesToDelete) {
        //[_projectiles removeObject:bird];
        //[self removeChild:bird cleanup:YES];
    }
    [projectilesToDelete release];

}
4

1 回答 1

1

好吧,您的 cloud 和 sol 对象都在同一个数组 _targets 中。因此,除非您在循环中测试正确的种类,否则您将无法对其中任何一个执行所需的操作。像这样(假设 cloud 和 sol 是 CCSprite 的子类):

for (CCSprite *cloudSprite in _targets) {
    if(![cloudSprite isKindOfClass:[Cloud class]]) continue;
    cloudSprite.anchorPoint = ccp(0, 0); 
    CGRect absoluteBox = CGRectMake(cloudSprite.position.x, cloudSprite.position.y, [cloudSprite boundingBox].size.width, [cloudSprite boundingBox].size.height);
    if (CGRectIntersectsRect([bird boundingBox], [cloudSprite boundingBox])) {
        [targetsToDelete addObject:cloudSprite];

    }                       
}

这样您只需将云添加到targetsToDelete。关键是在_targets 中区分云和溶胶。子类化是防弹的。否则你可以使用标签来实现它,但它可能是一个更脆弱的设计。一旦您可以区分 _targets 中的对象类型,就可以轻松优化此代码(_targets 和 targetsToDelete.

于 2012-05-01T16:56:48.227 回答