CCNode有三个方法,使用CCDirector的replaceScene方法替换场景时,每个节点都会调用这三个方法:

onEnter与onExit方法在改变场景过程中的特定时刻被调用,这取决于是否使用CCTransitionScene。

onEnterTransitionDidFinish方法在替换结束时调用。

必须总是调用这些方法的超类实现来避免难输入问题和内存泄漏。

01 -(void) onEnter 
02
03     CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self); 
04        
05     // must call super here: 
06     [super onEnter]; 
07
08    
09 -(void) onEnterTransitionDidFinish 
10
11     CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self); 
12        
13     // must call super here: 
14     [super onEnterTransitionDidFinish]; 
15
16    
17 -(void) onExit 
18
19     CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self); 
20        
21     // must call super here: 
22     [super onExit]; 
23 }

编写过渡场景:

LoadingScene充当一个中间场景的角色,它是cocos2d中的CCScene类的派生类,不必为每一次场景创建一个新的LoadingScene。

代码清单:LoadingScene.h

01 #import <Foundation/Foundation.h> 
02 #import "cocos2d.h" 
03    
04 typedef enum 
05
06     TargetSceneINVALID = 0, 
07     TargetSceneFirstScene, 
08     TargetSceneOtherScene, 
09     TargetSceneMAX, 
10 } TargetScenes; 
11    
12 <a href="http://my.oschina.net/interface" class="referer" target="_blank">@interface</a>  LoadingScene : CCScene { 
13        
14     TargetScenes targetScene_; 
15
16    
17 +(id) sceneWithTargetScene:(TargetScenes)targetScene; 
18 -(id) initWithTargetScene:(TargetScenes)targetScene; 
19    
20 <a href="http://my.oschina.net/u/567204" class="referer" target="_blank">@end</a>

使用enum给各个场景编个号,而且将enum的第一个值设为0,在Objective-c中变量的值会自动初始化为0.并在最后设了个MAX值。

代码清单:LoadingScene.m

01 #import "LoadingScene.h" 
02 #import "MyFirstScene.h" 
03 #import "OtherScene.h" 
04    
05 <a href="http://my.oschina.net/interface" class="referer" target="_blank">@interface</a>  LoadingScene (PrivateMethods) 
06 -(void) update:(ccTime)delta; 
07 <a href="http://my.oschina.net/u/567204" class="referer" target="_blank">@end</a>  
08    
09 @implementation LoadingScene 
10 +(id) sceneWithTargetScene:(TargetScenes)targetScene; 
11
12     CCLOG(@"==========================================="); 
13     CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self); 
14        
15     // This creates an autorelease object of self (the current class: LoadingScene) 
16     return [[[self alloc] initWithTargetScene:targetScene] autorelease]; 
17        
18     // Note: this does the exact same, it only replaced self with LoadingScene. The above is much more common. 
19     //return [[[LoadingScene alloc] initWithTargetScene:targetScene] autorelease]; 
20
21    
22 -(id) initWithTargetScene:(TargetScenes)targetScene 
23
24     if ((self = [super init])) 
25     
26         targetScene_ = targetScene; 
27            
28         CCLabelTTF* label = [CCLabelTTF labelWithString:@"Loading ..." fontName:@"Marker Felt" fontSize:64]; 
29         CGSize size = [[CCDirector sharedDirector] winSize]; 
30         label.position = CGPointMake(size.width / 2, size.height / 2); 
31         [self addChild:label]; 
32            
33         // Must wait one frame before loading the target scene! 
34         // Two reasons: first, it would crash if not. Second, the Loading label wouldn't be displayed. 
35         [self scheduleUpdate]; 
36     
37        
38     return self; 
39
40    
41 -(void) update:(ccTime)delta 
42
43     // It's not strictly necessary, as we're changing the scene anyway. But just to be safe. 
44     [self unscheduleAllSelectors]; 
45        
46     // Decide which scene to load based on the TargetScenes enum. 
47     // You could also use TargetScene to load the same with using a variety of transitions. 
48     switch (targetScene_) 
49     
50         case TargetSceneFirstScene: 
51             [[CCDirector sharedDirector] replaceScene:[MyFirstScene scene]]; 
52             break
53         case TargetSceneOtherScene: 
54             [[CCDirector sharedDirector] replaceScene:[OtherScene scene]]; 
55             break
56                
57         default
58             // Always warn if an unspecified enum value was used. It's a reminder for yourself to update the switch 
59             // whenever you add more enum values. 
60             NSAssert2(nil, @"%@: unsupported TargetScene %i", NSStringFromSelector(_cmd), targetScene_); 
61             break
62     
63        
64     // Tip: example usage of the INVALID and MAX enum values to iterate over all enum values 
65     for (TargetScenes i = TargetSceneINVALID + 1; i < TargetSceneMAX; i++) 
66     
67     
68
69    
70 -(void) dealloc 
71
72     CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self); 
73        
74     // don't forget to call "super dealloc" 
75     [super dealloc]; 
76
77 <a href="http://my.oschina.net/u/567204" class="referer" target="_blank">@end</a>

由于LoadingScene类由CCScene派生出来,所以不需要调用[CCScene node],sceneWithTargetScene方法先为self分配空间,然后调用initWithTargetScene初始化。

在MyFirstScene中调用LoadingScene:

1 CCScene *newScene = [LoadingScene sceneWithTargetScene:TargetSceneOtherScene]; 
2 [[CCDirector sharedDirector] replaceScene:newScene];

在OtherScene中调用基本类似,请读者自行实践。

下面给出了在执行过程中的一些输出信息,它详细的说明了各个方法的调用顺序:

01 2012-10-06 13:50:03.404 MutiScene[1884:1be03] =========================================== 
02 2012-10-06 13:50:03.405 MutiScene[1884:1be03] scene: MyFirstScene 
03 2012-10-06 13:50:03.406 MutiScene[1884:1be03] init : <MyFirstScene = 0x94e2540 | Tag = -1> 
04 2012-10-06 13:50:08.610 MutiScene[1884:1be03] cocos2d: animation started with frame interval: 60.00 
05 2012-10-06 13:50:08.613 MutiScene[1884:1be03] cocos2d: surface size: 480x320 
06 2012-10-06 13:50:08.614 MutiScene[1884:1be03] onEnter: <MyFirstScene = 0x94e2540 | Tag = -1> 
07 2012-10-06 13:50:08.615 MutiScene[1884:1be03] onEnterTransitionDidFinish: <MyFirstScene = 0x94e2540 | Tag = -1> 
08 2012-10-06 13:50:11.844 MutiScene[1884:1be03] =========================================== 
09 2012-10-06 13:50:11.845 MutiScene[1884:1be03] scene: OtherScene 
10 2012-10-06 13:50:11.846 MutiScene[1884:1be03] init: <OtherScene = 0x9418280 | Tag = -1> 
11 2012-10-06 13:50:16.907 MutiScene[1884:1be03] onEnter: <OtherScene = 0x9418280 | Tag = -1> 
12 2012-10-06 13:50:19.944 MutiScene[1884:1be03] onExit: <MyFirstScene = 0x94e2540 | Tag = -1> 
13 2012-10-06 13:50:19.945 MutiScene[1884:1be03] onEnterTransitionDidFinish: <OtherScene = 0x9418280 | Tag = -1> 
14 2012-10-06 13:50:19.947 MutiScene[1884:1be03] dealloc : <MyFirstScene = 0x94e2540 | Tag = -1> 
15 2012-10-06 13:50:29.953 MutiScene[1884:1be03] =========================================== 
16 2012-10-06 13:50:29.954 MutiScene[1884:1be03] sceneWithTargetScene:: LoadingScene 
17 2012-10-06 13:50:29.961 MutiScene[1884:1be03] onExit: <OtherScene = 0x9418280 | Tag = -1> 
18 2012-10-06 13:50:29.962 MutiScene[1884:1be03] dealloc: <OtherScene = 0x9418280 | Tag = -1> 
19 2012-10-06 13:50:29.977 MutiScene[1884:1be03] =========================================== 
20 2012-10-06 13:50:29.979 MutiScene[1884:1be03] scene: MyFirstScene 
21 2012-10-06 13:50:29.980 MutiScene[1884:1be03] init : <MyFirstScene = 0x9418280 | Tag = -1> 
22 2012-10-06 13:50:35.031 MutiScene[1884:1be03] dealloc: <LoadingScene = 0x11a59950 | Tag = -1> 
23 2012-10-06 13:50:35.032 MutiScene[1884:1be03] onEnter: <MyFirstScene = 0x9418280 | Tag = -1> 
24 2012-10-06 13:50:35.032 MutiScene[1884:1be03] onEnterTransitionDidFinish: <MyFirstScene = 0x9418280 | Tag = -1>

使用过渡场景在多个场景的切换COCOS2D(4)的更多相关文章

  1. loadrunner 运行场景-命令行运行场景

    运行场景-命令行运行场景 by:授客 QQ:1033553122 1 相对路径与绝对路径 在场景中为脚本指定一个相对位置,可以是相对于当前场景目录或lr安装目录. 当你运行一个场景,场景自动从这个相对 ...

  2. 搭建LoadRunner中的场景(三)场景的执行计划

    所谓场景操作,包括初始化用户组.启动用户组各用户以及停止虚拟用户的全过程.依据设置不同,执行过程中可以最多有5类操作,分别是启动用户组(start group).初始化(Initialize).启动虚 ...

  3. mongodb 使用场景和不使用场景

    1.mongodb介绍 MongoDB (名称来自"humongous") 是一个可扩展的高性能,开源,模式自由,面向文档的数据库.它使用C++编写.MongoDB特点: a.面向 ...

  4. 手工场景--controller--场景设计、场景监控、运行场景

    场景设置: 1.设置界面 2.全局设置. A:初始化: B:启动用户: C:

  5. Mycat适合场景及不适合场景

    1.非分片字段查询 Mycat中的路由结果是通过分片字段和分片方法来确定的.例如下图中的一个Mycat分库方案: 根据 tt_waybill 表的 id 字段来进行分片 分片方法为 id 值取 3 的 ...

  6. lr_场景设计之组场景、nmon监控

    1.组场景常用于回归 ,可以设置成一个脚本后多久运行下一个脚本: Real-world Schedule和Basic schedule的区别:根据官方文档,这两种模式下,场景中的每个虚拟用户组(可看成 ...

  7. 场景/故事/story——寻物者发布消息场景、寻失主发布消息场景、消息展示场景、登录网站场景

    1.背景:(1)典型用户:吴昭[主要]  尤迅[次要] 王丛[次要] 佑豪[次要](2)用户的需求/迫切需要解决的问题a.吴昭:经常在校园各个地方各个时间段,丢失物品需要寻找.b.吴昭:偶尔浏览一下最 ...

  8. Loadrunder场景设计篇——手工场景设计

    概述 通过选择需要运行的脚本,分配运行脚本的负载生成器,在脚本中分配Vuser来建立手工场景 手工场景就是自行设置虚拟用户的变化,主要是通过设计用户的添加和减少过程,来模拟真实的用户请求模型,完成负载 ...

  9. disruptor架构三 使用场景更加复杂的场景

    先c1和c2并行消费生产者产生的数据,然后c3再消费该数据 我们来使用代码实现:我们可以使用Disruptor实例来实现,也可以不用产生Disruptor实例,直接调用RingBuffer的api来实 ...

随机推荐

  1. mh

    http://video.sina.com.cn/vlist/news/zt/mlxyhkhbsl/#131455718 http://www.cnblogs.com/xinye/archive/20 ...

  2. javascript 事件设计模式

    http://plkong.iteye.com/blog/213543 1. 事件设计概述 事件机制可以是程序逻辑更加清晰可见,在JavaScript中很多对象都有自己的事件,如:button有onc ...

  3. NFC-P2P MODE

    今日看见有关国内电信业者要合组TSM (Trusted Service Manager)提供NFC 服务的新闻, 这是属于NFC 所能提供的3种Mode中的Card emulation mode (就 ...

  4. [Leetcode][Python]47: Permutations II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...

  5. PHP实现好友生日邮件提醒

    我有一个想法是这样的,希望每天知道今天是我哪位好友的生日? 当然,我得首先保存我所有好友的生日信息 实现:在新浪申请免费服务器,并申请Mysql应用,然后建一张表保存好友的信息 我想,我已经完成第一步 ...

  6. 超文本传输协议-HTTP/1.1

    超文本传输协议-HTTP/1.1(修订版) ---译者:孙超进本协议不限流传发布.版权声明Copyright (C) The Internet Society (1999). All Rights R ...

  7. UITableView 或 UIScrollView 点击状态栏列表回到顶部

    整理来自互联网- 这是tableView继承的scrollView的一个属性 scrollsToTop. 官方说明是这样的: // When the user taps the status bar, ...

  8. CXF发布webservice入门

    1.设置CXF的bin目录进环境变量 2.CXF导入相关的jar包. 3.建立接口 @WebService public interface HelloWorld { public void say( ...

  9. Basic Concepts of Block Media Recovery

    Basic Concepts of Block Media Recovery Whenever block corruption has been automatically detected, yo ...

  10. zepto 获取checked selected元素

    原文阅读:WISER CODER 1. Zepto.js and the :selected and :checked selectors 如果你已经看上了jQuery那残弱的表兄弟, Zepto.j ...