1

Trying to integrate a Cocos2d view into a UIKit app. I'm getting this error:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Do not initialize the TextureCache before the Director'

It seems to me that the Director is being initialized first.

AppDelegate didFinishLaunching:

// initialize cocos2d director
    CCDirectorIOS* director = (CCDirectorIOS*)[CCDirector sharedDirector];
    director.wantsFullScreenLayout = NO;
    director.projection = kCCDirectorProjection2D;
    director.animationInterval = 1.0 / 60.0;
    director.displayStats = YES;
    [director enableRetinaDisplay:YES];

    NSArray* subviews = self.viewController.view.subviews;
    for (int i = 0; i < subviews.count; i++)
    {
        UIView* subview = [subviews objectAtIndex:i];
        if ([subview isKindOfClass:[CCGLView class]])
        {
            director.view = (CCGLView*)subview;
            break;
        }
    }

View Controller viewDidLoad:

CCDirectorIOS* director = (CCDirectorIOS*)[CCDirector sharedDirector];
    if (director.runningScene == nil)
    {
        [director runWithScene:[GameLayer scene]];
    }
    [director startAnimation];

And GameLayer:

+(id) scene {
    CCScene *scene=[CCScene node];
    CCLayer* layer=[GameLayer node];
    [scene addChild:layer];
    return scene;
}
-(id) init {
    if ((self=[super init])) {

        CCSprite *test = [CCSprite spriteWithFile:@"test.png"];
        [self addChild:test];

        CGSize size = [CCDirector sharedDirector].winSize;

        test.position = CGPointMake(size.width / 2, size.height / 2);
    }
    return self;
}

What am I doing wrong?

soleil
  • 12,133
  • 33
  • 112
  • 183
  • what's the call stack when this error happens? Enable global exception breakpoint in Xcode. – CodeSmile Mar 02 '13 at 00:26
  • The call stack is ViewController viewDidLoad > GameLayer scene > CCNode node > GameLayer init > CCSprite spriteWithFile > CCSprite initWithFile > CCTextureCache sharedTextureCache > CCTextureCache init – soleil Mar 02 '13 at 00:47

1 Answers1

2

I see it now: in didFinishLaunching you forgot to create the CCGLView instance and assign it to CCDirector. Your director is running without a view, and has no OpenGL context. Therefore textures can't be created.

The real issue is the misleading error message.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • Marking this as the correct answer, but honestly I've run into so many issues trying to do this that it's hard to say what has fixed it. The only way I could get it to work at all is to start with your CH15 ViewBasedAppWithCocos2D project and copy all the files from my original Xcode project over to that one. Still having issues with starting the Cocos2d view a 2nd time, but I'll make that a separate question. – soleil Mar 02 '13 at 17:39