0

所以在我的游戏中,我有物品从屏幕顶部掉下来,当玩家(另一个精灵)抓住这些物品时,该物品会消失并在计数器上添加一个。

这是我的碰撞检查方法

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

任何帮助表示赞赏,但请不要像某些人那样只说“去学习objective-C”:/

4

1 回答 1

0

在您的代码中,您编写了这些行,

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

我不明白你为什么添加这个,所以我认为有错误所以请检查,否则解释你为什么添加这个代码..

于 2012-07-24T13:18:38.820 回答