我在CCNode中添加和删除CCScrollView,如下所示:
-(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;
}
}
一切正常,但当我openShop
运行removeChild:scroll
时,程序崩溃并返回此错误:Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'This node does not contain the specified child.'
我不明白,因为scroll
显然添加到孩子..那么scrollContainer
如何不包含scroll
?
答案 0 :(得分:0)
看看你的代码,它告诉你答案。您创建滚动并在创建后立即询问'确实包含滚动:当然没有任何内容包含它,因为您尚未将其添加到任何内容中。相反,试试这个:
-(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;
}
}
使用任何cocos2d的CCNode后代的'name'属性对您有利。我假设你使用的是cocos的3.x版。
ob p:obcit:从内存来看,不确定这会编译,但它会给你一般的想法。