碰撞检查问题

时间:2012-07-23 06:03:09

标签: cocos2d-iphone collision-detection sprite

所以在我的游戏中,我有从屏幕顶部掉落的物品,当玩家(另一个精灵)抓住物品时,物品消失并向柜台添加一个物品。

这是我的碰撞检查方法

//WHEN THE THINGS COLLIDE, THEY DISSAPEAR
- (void)update:(ccTime)dt {
CGSize winSize = [[CCDirector sharedDirector] winSize];

NSMutableArray *targetsToDelete = [[NSMutableArray alloc] init];
for (CCSprite *target in _targets) {
    CGRect targetRect = CGRectMake(
                                       target.position.x - (target.contentSize.width/2),
                                       target.position.y - (target.contentSize.height/2),
                                       target.contentSize.width,
                                       target.contentSize.height);
    BOOL playerHit = FALSE;
        CGRect playerRect = CGRectMake(
                                       _Banker.position.x - (_Banker.contentSize.width/2),
                                       _Banker.position.y -     (_Banker.contentSize.height/2),
                                       _Banker.contentSize.width,
                                       _Banker.contentSize.height);

        if (CGRectIntersectsRect(playerRect, targetRect)) {
            //[targetsToDelete addObject:target];
            playerHit = TRUE;
                [targetsToDelete addObject:target];

            break;

    }

    for (CCSprite *target in targetsToDelete) {
        [_targets removeObject:target];
        [self removeChild:target cleanup:YES];

        _targetsDestroyed++;
        [_label setString:[NSString stringWithFormat:@""]];
        if (_targetsDestroyed > 30) {
            GameWinScene *gameWinScene = [GameWinScene node];
            _targetsDestroyed = 0;
            [[CCDirector sharedDirector] replaceScene:gameWinScene];
        } else{
            NSString *killcounttext = [NSString stringWithFormat:@"Catches: %i",     _targetsDestroyed];
            self.label = [CCLabelTTF labelWithString:killcounttext fontName:@"Zapfino"     fontSize:20];
            _label.color = ccc3(225,225,225);
            _label.position = ccp(winSize.width * 0.20,winSize.height * 0.92);
            [self addChild:_label];
        }
    }

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

我添加了CCLOG以确保将目标添加到targetstodelete,因为在我的下一个方法中我删除了targetstodelete中的任何内容。 CCLOG确认目标正在添加,但不会被删除。

这是我的删除方法

for (CCSprite *target in targetsToDelete) {
        [_targets removeObject:target];
        [self removeChild:target cleanup:YES];

        _targetsDestroyed++;
        [_label setString:[NSString stringWithFormat:@""]];
        if (_targetsDestroyed > 30) {
            GameWinScene *gameWinScene = [GameWinScene node];
            _targetsDestroyed = 0;
            [[CCDirector sharedDirector] replaceScene:gameWinScene];
        } else{
            NSString *killcounttext = [NSString stringWithFormat:@"Catches: %i", _targetsDestroyed];
            self.label = [CCLabelTTF labelWithString:killcounttext fontName:@"Zapfino" fontSize:20];
            _label.color = ccc3(225,225,225);
            _label.position = ccp(winSize.width * 0.23,winSize.height * 0.92);
            [self addChild:_label];
        }
    }

任何帮助都表示赞赏,但请不要只说“去学习目标-C”就像有些人一样:/

1 个答案:

答案 0 :(得分:0)

在你的代码中你写了这行,

 if (targetsToDelete.count > 0) {
        [targetsToDelete addObject:target];
    }

我无法理解你为什么添加这个,所以我认为有错误所以请另外解释为什么你要添加这个代码..