0
votes

I am adding and removing a CCScrollView to and from a CCNode as so:

-(void)openShop
{
    CCNode *shopNode = [CCBReader loadAsScene:@"Shop"];
    CCScrollView *scroll = [[CCScrollView alloc]initWithContentNode:shopNode];
    if (visible == NO) {
        [shopNode setContentSizeInPoints:(CGSizeMake(320, 1000))];
        [scrollContainer addChild:scroll];
        [scroll setContentSizeInPoints:(CGSizeMake(320.0, 370.0))];
        [scroll setHorizontalScrollEnabled:NO];
        [scroll setPosition:(CGPointMake(0, 70))];
        [scroll setScrollPosition:(CGPointMake(0, 0))];
        visible = YES;
    } else {
        [scrollContainer removeChild:scroll];
        visible = NO;
    }
}

Everything displays fine, but when I run openShop to removeChild:scroll the program crashes and returns this error: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'This node does not contain the specified child.' I don't understand because scroll is obviously added to the child.. So how can scrollContainer not contain scroll?

1
Try [scroll removeFromParentAndCleanup:YES] instead, it's the safer method for removing nodes. Though if you set a breakpoint you can probably deduce from call stack and variable values where it is going wrong. - LearnCocos2D
using [scroll removeFromParentAndCleanup:YES] doesn't crash the program anymore, but the scrollview doesn't disappear. I set up a breakpoint, but should I be looking for? Thanks - SaleenS7
What are the children of scrollContainer? - i_am_jorf
There are no other children of scrollContainer other than scroll when it is added in the code above - SaleenS7

1 Answers

0
votes

Look at your code, it is telling you the answer. You create scroll and immediately after creating ask 'does something contain scroll : of course nothing contains it, since you have not added it to anything yet. Instead try this:

-(void)openShop
{    
     if([scrollContainer containsChildByName:@"scrollName" recursively:NO]) {
         [scrollContainer removeChildByName:@"scrollName"];
         visible = NO;
     } else {
         CCNode *shopNode = [CCBReader loadAsScene:@"Shop"];
         CCScrollView *scroll = [[CCScrollView alloc]initWithContentNode:shopNode];
         [shopNode setContentSizeInPoints:(CGSizeMake(320, 1000))];
         [scrollContainer addChild:scroll z:0 name:@"scrollName"];
         [scroll setContentSizeInPoints:(CGSizeMake(320.0, 370.0))];
         [scroll setHorizontalScrollEnabled:NO];
         [scroll setPosition:(CGPointMake(0, 70))];
         visible = YES;
     }
}

use the 'name' property of any cocos2d's CCNode descendant to your advantage. I am assuming you are using cocos's version 3.x.

obcit : from memory, not certain this compiles but it gives you the general idea.