CCNode有三个方法,使用CCDirector的replaceScene方法替换场景时,每个节点都会调用这三个方法:
onEnter与onExit方法在改变场景过程中的特定时刻被调用,这取决于是否使用CCTransitionScene。
onEnterTransitionDidFinish方法在替换结束时调用。
必须总是调用这些方法的超类实现来避免难输入问题和内存泄漏。
03 |
CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self); |
05 |
// must call super here: |
09 |
-(void) onEnterTransitionDidFinish |
11 |
CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self); |
13 |
// must call super here: |
14 |
[super onEnterTransitionDidFinish]; |
19 |
CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self); |
21 |
// must call super here: |
编写过渡场景:
LoadingScene充当一个中间场景的角色,它是cocos2d中的CCScene类的派生类,不必为每一次场景创建一个新的LoadingScene。
代码清单:LoadingScene.h
01 |
#import <Foundation/Foundation.h> |
06 |
TargetSceneINVALID = 0, |
07 |
TargetSceneFirstScene, |
08 |
TargetSceneOtherScene, |
12 |
<a href="http://my.oschina.net/interface" class="referer" target="_blank">@interface</a> LoadingScene : CCScene { |
14 |
TargetScenes targetScene_; |
17 |
+(id) sceneWithTargetScene:(TargetScenes)targetScene; |
18 |
-(id) initWithTargetScene:(TargetScenes)targetScene; |
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" |
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> |
09 |
@implementation LoadingScene |
10 |
+(id) sceneWithTargetScene:(TargetScenes)targetScene; |
12 |
CCLOG(@"==========================================="); |
13 |
CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self); |
15 |
// This creates an autorelease object of self (the current class: LoadingScene) |
16 |
return [[[self alloc] initWithTargetScene:targetScene] autorelease]; |
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]; |
22 |
-(id) initWithTargetScene:(TargetScenes)targetScene |
24 |
if ((self = [super init])) |
26 |
targetScene_ = targetScene; |
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]; |
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]; |
41 |
-(void) update:(ccTime)delta |
43 |
// It's not strictly necessary, as we're changing the scene anyway. But just to be safe. |
44 |
[self unscheduleAllSelectors]; |
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. |
50 |
case TargetSceneFirstScene: |
51 |
[[CCDirector sharedDirector] replaceScene:[MyFirstScene scene]]; |
53 |
case TargetSceneOtherScene: |
54 |
[[CCDirector sharedDirector] replaceScene:[OtherScene scene]]; |
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_); |
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++) |
72 |
CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self); |
74 |
// don't forget to call "super dealloc" |
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> |
- loadrunner 运行场景-命令行运行场景
运行场景-命令行运行场景 by:授客 QQ:1033553122 1 相对路径与绝对路径 在场景中为脚本指定一个相对位置,可以是相对于当前场景目录或lr安装目录. 当你运行一个场景,场景自动从这个相对 ...
- 搭建LoadRunner中的场景(三)场景的执行计划
所谓场景操作,包括初始化用户组.启动用户组各用户以及停止虚拟用户的全过程.依据设置不同,执行过程中可以最多有5类操作,分别是启动用户组(start group).初始化(Initialize).启动虚 ...
- mongodb 使用场景和不使用场景
1.mongodb介绍 MongoDB (名称来自"humongous") 是一个可扩展的高性能,开源,模式自由,面向文档的数据库.它使用C++编写.MongoDB特点: a.面向 ...
- 手工场景--controller--场景设计、场景监控、运行场景
场景设置: 1.设置界面 2.全局设置. A:初始化: B:启动用户: C:
- Mycat适合场景及不适合场景
1.非分片字段查询 Mycat中的路由结果是通过分片字段和分片方法来确定的.例如下图中的一个Mycat分库方案: 根据 tt_waybill 表的 id 字段来进行分片 分片方法为 id 值取 3 的 ...
- lr_场景设计之组场景、nmon监控
1.组场景常用于回归 ,可以设置成一个脚本后多久运行下一个脚本: Real-world Schedule和Basic schedule的区别:根据官方文档,这两种模式下,场景中的每个虚拟用户组(可看成 ...
- 场景/故事/story——寻物者发布消息场景、寻失主发布消息场景、消息展示场景、登录网站场景
1.背景:(1)典型用户:吴昭[主要] 尤迅[次要] 王丛[次要] 佑豪[次要](2)用户的需求/迫切需要解决的问题a.吴昭:经常在校园各个地方各个时间段,丢失物品需要寻找.b.吴昭:偶尔浏览一下最 ...
- Loadrunder场景设计篇——手工场景设计
概述 通过选择需要运行的脚本,分配运行脚本的负载生成器,在脚本中分配Vuser来建立手工场景 手工场景就是自行设置虚拟用户的变化,主要是通过设计用户的添加和减少过程,来模拟真实的用户请求模型,完成负载 ...
- disruptor架构三 使用场景更加复杂的场景
先c1和c2并行消费生产者产生的数据,然后c3再消费该数据 我们来使用代码实现:我们可以使用Disruptor实例来实现,也可以不用产生Disruptor实例,直接调用RingBuffer的api来实 ...
随机推荐
- Delphi获取系统服务描述信息
program Project1; {$APPTYPE CONSOLE} uses Windows, WinSvc; type SERVICE_DESCRIPTION = packed record ...
- 切图教程,APP切图实例
- 使用xmanager 远程redhat6.3
之前装过一次,特别麻烦,装上只有远程还卡卡的,这次按照教程居然装的灰常顺利,不符合我bug体质的特性,一定要记下来啊~~~ 1.先关闭防火墙 # service iptables stop #chkc ...
- UITableView 或 UIScrollView 点击状态栏列表回到顶部
整理来自互联网- 这是tableView继承的scrollView的一个属性 scrollsToTop. 官方说明是这样的: // When the user taps the status bar, ...
- C++暂时对象
C++真正所谓的暂时对象是不可见的--不会在你的源码中出现.仅仅要你产生一个non-heap object而没有为它命名,便诞生了一个暂时对象.此等匿名对象通常发生于两种情况: 一是当隐式类型转换(i ...
- 通过设置cookie实现单点登录
最近要做个登录一个客户端跳转到另一个网站不用再登录,有两种方法,第一种就是写接口通过客户端传值账号直接到目标网站,另一种是写入cookie到目标网站.由于目标网站之前就是通过cookie实现单点登录, ...
- VMware下桥接设置
操作环境 主机:Win7 X86 SP1 虚拟机:VMware station 8 虚拟机里的系统:Fedora 15 环境上,不管什么系统,什么版本的虚拟机,使用上都是大同小异的,毕竟核心是不变的. ...
- Genymotion error:The virtual device got no IP address
控制面板,网络和intent,网络和共享中心,更改适配器设置,看下你的VirtualBox Host-Only Ethernet Adapter这个显卡 启动了没有, 没有就启动它!!!
- 使用UTL_SMTP发送中文电子邮件
就是在原有TOM源码的基础上修改utl_smtp.write_data中,将输出内容进行一下数据转换,这样可以保证中文输出不会出现乱码 ----------------------------- cr ...
- iOS开发之git学习
本人是参考廖雪峰的git学习的.他写的非常详细,我在这里就是把我学习中的总结以及碰到的坑写出来. /* 初始化git仓库:git init */ /* 添加文件到git仓库 */ 分两步: 第一步:追 ...