1

我为每个身体添加一个精灵并运行动画,但是当我尝试在重新加载场景后删除这个精灵时(再次调用 afterLoadProcessing) - 删除了唯一的第一个动画。我是 iOS 和 Cococs2D 编程的新手,请帮助我。

这是我的代码:

-(void)afterLoadProcessing:(b2dJson*)json
{
    [super afterLoadProcessing:json];

    [self removeChild:enemysprite cleanup:YES];

    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"player_enemy.plist"];
    CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"player_enemy.png"];
    [self addChild:spriteSheet];

NSMutableArray *enemyGusenitsaAnimFrames = [NSMutableArray array];
    for (int i = 31; i <=36; ++i) {
        [enemyGusenitsaAnimFrames addObject:
         [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
          [NSString stringWithFormat:@"%d.png",i]]];
    }
    CCAnimation *gusenitsaMovAnim = [CCAnimation animationWithSpriteFrames:enemyGusenitsaAnimFrames delay:0.1f];
    _enemyGusenitsaMovement = [CCRepeatForever actionWithAction:
                                    [CCAnimate actionWithAnimation:gusenitsaMovAnim]];
    _enemyGusenitsaMovement.tag = 109;

std::vector<b2Body*> enemySprites;
    json->getBodiesByName("enemy", enemySprites);
    for (int i = 0; i < enemySprites.size(); i++) {
        b2Body *bod = enemySprites[i];
        enemysprite = [[CCSprite alloc] init];

        [enemysprite stopAllActions];
        enemysprite.scale = 0.04;
        [self addChild:enemysprite z:50];
        bod->SetUserData(enemysprite);
        [[enemysprite runAction:[_enemyGusenitsaMovement copy]] autorelease];
    }
    }
4

1 回答 1

0

[self removeChild:enemysprite cleanup:YES];您可以删除所有子项,而不是使用

 [self removeAllChildrenWithCleanup:YES];

或循环所有主体并将其删除:

for (int i = 0; i < enemySprites.size(); i++) {
    b2Body *bod = enemySprites[i];
    CCSprite *sprite = (CCSprite *)bod->GetUserData();
    [self removeChild:sprite];
}
于 2013-07-01T14:12:49.953 回答