cocos2d - NSMutableArray 与级别,重新启动级别和崩溃,下一个级别工作良好



我有带有级别和两个按钮的nsmutablearray。下一关,再试一次。当用户触摸下一个级别时,我设置了objectAtIndex:indexCurrLevel+1,下一个级别加载没有问题。但是,如果用户触摸,请重试,我尝试将级别设置为对象索引:索引CurrLevel应用程序崩溃,并显示儿童已添加错误。这太疯狂了,因为如果我手动设置,请重试 objectAtIndex:5 完美运行,直到用户无法在 5 级别上播放,因为应用程序崩溃了。

对于与当前级别索引不同的索引,可以完美运行。(objectAtIndex:index - works)手动设置索引给出了与索引相同的原因。

[self removeFromParentAndCleanup:YES];
    Levels *l = [levels2 objectAtIndex:index-1];
    Game *hl = [[Game alloc]initWithObstacles:[l staticO] :[l rotateO]:[l lvl]:[l level]:[l pendulumO]:self.levelsArray];
    [hl setLevels2:self.levels2];
    [hl setBasketY:[l basketY]];
    [hl setBasketX:[l basketX]];

    [l release];
    [[CCDirector sharedDirector] replaceScene:(CCScene*) hl];    
    [hl configureLevel];
    [hl release];

Eroor:-[Game addChild:z:tag:],../libs/cocos2d/CCNode.m:3882012-05-11 19:03:20.349 游戏[932:10a03] * 由于未捕获的异常"NSInternalInconsistencyException"而终止应用程序,原因:"子项已添加。无法再次添加"* 第一次抛出调用堆栈:

猜测数组边界问题,但没有代码示例或崩溃日志就无法验证。

尝试删除语句[l release]。 通常,您应该只释放已调用retain的对象或使用以 initnewcopy 开头的方法获得的对象。 数组levels2也将保留每个级别,将其保留为级别的所有者可能是合适的。 在 Xcode 中运行静态分析器来解决这些问题。

阅读消息:您正在尝试添加已经具有父对象的Child...即已作为 CCNode 后代的子代添加到代码库的其他地方。CCNode 的第 388 行是 NSAssert,不可破解。暂时更改 CCNode 以具有可破解的指令,如下所示:

if (nil==child) {
    CCLOG(@"%@<addChild> : have nil child. not adding.",self.class);
    return;
}
if (child.parent) {
    CCLOG(@"%@<addChild> : This child is already added somewhere. not adding.",self.class); // **** PUT BREAKPOINT HERE *****//
    return;
}
NSAssert( child != nil, @"Argument must be non-nil"); // was line 388
NSAssert( child.parent == nil, @"child already added. It can't be added again");

最后 2 行来自 CCNode(在我的项目中)。如果您提到的错误发生,我会在指示的行处放置一个中断,重新运行并查看堆栈跟踪。当你完成了你的开发/测试周期,这些事情对你很重要,记得将 cocos2d 恢复到此类临时模组的原始状态。

最新更新