본문 바로가기

옛글/아이폰 프로그래밍

child already added. It can't be added again COCOS2D CCNode에러

반응형

 COCOS2D 의 CCnode에서 발생하는 에러로 


Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'child already added. It can't be added again'



위와 같은 에러메세지를 출력하는 경우, Child가 중첩되어 Add 를 하거나 Child의 Parent가 존재하지 않는데 추가를 하려고 시도하는 경우 발생한다. 


-(void) addChild: (CCNode*) child z:(NSInteger)z tag:(NSInteger) aTag

{

    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");

NSAssert( child.parent == nil, @"child already added. It can't be added again");


CCnode내에 위에 Return 을 해주면 에러를 발생시키며 앱이 죽는 경우는 생기지 않는다. 
(물론 Child.parent가 잘못된경우에는 Return 이 맞겠지만, 좀 더 공부를 해봐야할 듯)

아래 문서도 참조하면 좋을듯. 

http://stackoverflow.com/questions/7639409/cocos2d-child-already-added-it-cant-be-added-again


반응형