removeChildByTag:找不到子级错误

时间:2013-09-14 14:43:30

标签: iphone cocos2d-iphone

removeChildByTag:找不到孩子这是我的代码,请帮忙! 我是cocos2d的一个相当新的开发人员所以我无法解决导致Xcode给我这个错误的原因。

//Life System
        CCSprite *Life3 = [CCSprite spriteWithFile:@"heart.png"];
        Life3.position = ccp(210,200);
        CCSprite *Life2 = [CCSprite spriteWithFile:@"heart.png"];
        Life2.position = ccp(220,200);
        CCSprite *Life1 = [CCSprite spriteWithFile:@"heart.png"];
        Life1.position = ccp(230,200);
        [self addChild:Life3];
        [self addChild:Life2];
        [self addChild:Life1];
        if(Life == 2) {
            [self removeChildByTag:Life3];
        }
        else if(Life == 1) {
            [self removeChild:Life2];
            [self removeChild:Life3];
        }
        else if(Life <= 0) {
            [self removeChild:Life1];
            [self removeChild:Life2];
            [self removeChild:Life3];
            [[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0 scene:[MainMenu scene]]];
        }

3 个答案:

答案 0 :(得分:0)

在条件为生命== 2,它应该是removeChild,而不是removeChildByTag

答案 1 :(得分:0)

我已经以正确的方式发布了您的代码,以便通过标记删除子项 removeChildByTag需要一个整数标记,而不是一个对象(在你的情况下是一个精灵)

    CCSprite *Life3 = [CCSprite spriteWithFile:@"heart.png"];
    Life3.tag = 3;
    Life3.position = ccp(210,200);

    CCSprite *Life2 = [CCSprite spriteWithFile:@"heart.png"];
    Life2.tag = 2;
    Life2.position = ccp(220,200);

    CCSprite *Life1 = [CCSprite spriteWithFile:@"heart.png"];
    Life1.tag = 1;
    Life1.position = ccp(230,200);

    [self addChild:Life3];
    [self addChild:Life2];
    [self addChild:Life1];

    if(Life == 2) {
        [self removeChildByTag:3];
    }
    else if(Life == 1) {
        [self removeChild:2];
        [self removeChild:1];
    }
    else if(Life <= 0) {
        [self removeChild:1];
        [self removeChild:2];
        [self removeChild:3];
        [[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0 scene:[MainMenu scene]]];
    }

答案 2 :(得分:0)

尝试用

替换所有[self removeChild:]或[self removeChildbyTag:]

[self removeChild:Life1 cleanup:YES]

如果这也不起作用,我建议你在需要的时候[self addChild:]。不要在开头添加所有三个Lifes作为孩子。所以代码将成为:

CCSprite *Life3 = [CCSprite spriteWithFile:@"heart.png"];
    Life3.position = ccp(210,200);
    CCSprite *Life2 = [CCSprite spriteWithFile:@"heart.png"];
    Life2.position = ccp(220,200);
    CCSprite *Life1 = [CCSprite spriteWithFile:@"heart.png"];
    Life1.position = ccp(230,200);

    if(Life == 2) {
        [self addChild:Life2];
        [self addChild:Life1];
    }
    else if(Life == 1) {
        [self addChild:Life1];
    }
    else if(Life <= 0) {
       [self addChild:Life3];
       [self addChild:Life2];
       [self addChild:Life1];
        [[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0 scene:[MainMenu scene]]];
    }