1

尝试将 Cocos2d 视图集成到 UIKit 应用程序中。我收到此错误:

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

在我看来,Director 是首先被初始化的。

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;
        }
    }

视图控制器 viewDidLoad:

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

和游戏层:

+(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;
}

我究竟做错了什么?

4

1 回答 1

2

我现在看到了:在 didFinishLaunching 中,您忘记创建 CCG​​LView 实例并将其分配给 CCDirector。您的导演在没有视图的情况下运行,并且没有 OpenGL 上下文。因此无法创建纹理。

真正的问题是误导性的错误信息。

于 2013-03-02T11:29:18.763 回答