zoukankan      html  css  js  c++  java
  • 使用过渡场景在多个场景的切换COCOS2D(4)

    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>
  • 相关阅读:
    批量转外部样式为行内样式
    AngularJS 笔记2
    windows特殊文件或文件夹
    zepto 入门
    js闭包
    AngularJS 笔记1
    sublime配置 sublimecondeintel 分号后不要提示
    sublime插件开发手记
    用flvplayer.swf在网页中播放视频(网页中flash视频播放的实现)
    无插件启动谷歌浏览器
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3212513.html
Copyright © 2011-2022 走看看