loadingLayer=[LoadingLayer node];
谛力泰克网 :app定制 http://www.dilitech.com
[self addChild:loadingLayer];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_async(queue, ^{
//声明一个Pool来自动管理指针
NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
//加锁保护,并保证与Cocos2d本身的异步加载函数本身不冲突
//也就是保护ShareGroup不被同时修改
// [[CCTextureCache sharedTextureCache].contextLock lock];
//auxGLcontext必须是static
//
if( auxGLcontext == nil ) {
//新建一个EAGLContext 然后与已经生成与使用的OpenGL公用一个ShareGroup
auxGLcontext = [[EAGLContext alloc]initWithAPI:kEAGLRenderingAPIOpenGLES1 sharegroup:[[[[CCDirector sharedDirector] openGLView] context] sharegroup]];
//检查是否
if( ! auxGLcontext ) CCLOG(@"cocos2d: TextureCache: Could not create EAGL context");
}
CCScene *scene;
//设置Context,拥有修改权利,接下来我们就可以正常的使用我们生成我们自己的Scene了
if( [EAGLContext setCurrentContext:auxGLcontext] ) {
//初始化
scene = [LevelController scene];
[[CCDirector sharedDirector] replaceScene:scene];
}
//回到主线程,保留并解锁,然后等等主线程接收方响应并开始切换场景
dispatch_async(dispatch_get_main_queue(), ^{
//防止指针丢失
// nextScene = [scene retain];
//lock用来判断scene是否生存完
// lock = NO;
});
//恢复
[EAGLContext setCurrentContext:nil];
// [[CCTextureCache sharedTextureCache].contextLock unlock];
[autoreleasepool release];
});
复制代码
如果用下列常规方式回出现场景图片与动画均显示不了的问题
loadingLayer=[LoadingLayer node];
[self addChild:loadingLayer];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
CCScene *levelScene = [LevelController scene];
[[CCDirector sharedDirector] replaceScene:levelScene];
dispatch_async(dispatch_get_main_queue(), ^{
[loadingLayer removeFromParentAndCleanup:YES];
});
});
复制代码 文章出处:谛力泰克网 app开发 http://www.dilitech.com