尝试将 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;
}
我究竟做错了什么?