您的位置:首页 > 移动开发

block实现场景切换

2013-02-21 16:50 435 查看
1        loadingLayer=[LoadingLayernode];

2         

3          谛力泰克网:app定制http://www.dilitech.com

4            [self addChild:loadingLayer];

5            dispatch_queue_t queue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);

6            dispatch_async(queue, ^{

7                //声明一个Pool来自动管理指针

8                NSAutoreleasePool *autoreleasepool =[[NSAutoreleasePool alloc] init];

9                

10             //加锁保护,并保证与Cocos2d本身的异步加载函数本身不冲突

11             //也就是保护ShareGroup不被同时修改

12     //       
[[CCTextureCachesharedTextureCache].contextLock lock];

13             

14             //auxGLcontext必须是static

15             //

16             if( auxGLcontext == nil ) {

17                 //新建一个EAGLContext然后与已经生成与使用的OpenGL公用一个ShareGroup

18                 auxGLcontext = [[EAGLContextalloc]initWithAPI:kEAGLRenderingAPIOpenGLES1 sharegroup:[[[[CCDirectorsharedDirector] openGLView] context]
sharegroup]];

19                 

20                 //检查是否

21                 if( ! auxGLcontext )CCLOG(@"cocos2d: TextureCache: Could not create EAGL context");

22             }

23             

24             CCScene *scene;

25             

26             //设置Context,拥有修改权利,接下来我们就可以正常的使用我们生成我们自己的Scene了

27             if( [EAGLContextsetCurrentContext:auxGLcontext] ) {

28                 //初始化

29                 scene = [LevelController scene];

30                 [[CCDirector sharedDirector]replaceScene:scene];

31             }

32             

33             //回到主线程,保留并解锁,然后等等主线程接收方响应并开始切换场景

34             dispatch_async(dispatch_get_main_queue(),^{

35                 //防止指针丢失

36     //           
nextScene = [scene retain];

37                 //lock用来判断scene是否生存完

38     //           
lock = NO;

39             });

40             

41             //恢复

42             [EAGLContext setCurrentContext:nil];

43     //    
   [[CCTextureCachesharedTextureCache].contextLock unlock];

44             [autoreleasepool release];

45         });

复制代码

如果用下列常规方式回出现场景图片与动画均显示不了的问题

46     loadingLayer=[LoadingLayernode];

47         [self addChild:loadingLayer];

48     dispatch_queue_t queue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

49     dispatch_async(queue, ^{

50             CCScene *levelScene = [LevelControllerscene];

51             [[CCDirector sharedDirector]replaceScene:levelScene];

52     dispatch_async(dispatch_get_main_queue(),^{

53                 [loadingLayer removeFromParentAndCleanup:YES];

54             });

55         });

复制代码

 

 

 

  文章出处:谛力泰克网 app开发 http://www.dilitech.com
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息