2013年1月18日金曜日

Sceneの遷移学習。なるほど。

◆画面の遷移を実装するために、Cocos2dのSceneTestのソースを見ながら学習してみる。(とても勉強になりました。Cocos2dに感謝。)

◆全体として、 layer1,layer2,layer3という三つのlayerクラスを@implementationして、準備し、pushSceneで遷移させたりしていますね。一つのファイルの中で、複数のクラスを実装し、それぞれで、sceneをつくって、その上にlayerをaddChildし、pushやreplaceする、という形で遷移を実現。なるほど。

◆ちなみに、どうも、pushSceneWithTransitionはKobold2dでは廃止の様子。メモリリーク対策のようで。以下、参考。
http://qiita.com/items/23fb37944c12d39f4de5 
pushでtransitionが使えないのは、ちょっと痛いですね。メモリを食うことを抑える、ということで。replaceSceneを使うのみ、ということですか。transitionを所望するなら。Kobold2dを使いそうなので、そうします。

◆以下、ソース引用とメモです。(改行が乱れているのは、bloggerのご愛敬で)
//
// Scene demo
// a cocos2d example
// http://www.cocos2d-iphone.org//
#import "SceneTest.h"
#pragma mark -
#pragma mark Layer1
@implementation Layer1-(id) init
{
if( (self=[super initWithColor: ccc4(0,255,0,255)]) ) {
(中略)
-(void) onPushScene: (id) sender
{
CCScene * scene = [CCScene node];
[scene addChild: [Layer2 node] z:0];//Layer2をaddChildして、
[[CCDirector sharedDirector] pushScene: scene];//pushSceneでプッシュして、sceneを切り替え。// [[Director sharedDirector] replaceScene:scene];
}
-(void) onPushSceneTran: (id) sender
{
CCScene * scene = [CCScene node];
[scene addChild: [Layer2 node] z:0];
[[CCDirector sharedDirector] pushScene: [CCTransitionSlideInT transitionWithDuration:1 scene:scene]];}

-(void) onQuit: (id) sender
{
[[CCDirector sharedDirector] popScene];[[CCDirector sharedDirector] end];}
-(void) onVoid: (id) sender
{
}
@end
#pragma mark -
#pragma mark Layer2
@implementation Layer2-(id) init
{
if( (self=[super initWithColor: ccc4(255,0,0,255)]) ) {
timeCounter = 0;
CCMenuItemFont *item1 = [CCMenuItemFont itemWithString: @"replaceScene" target:self selector:@selector(onReplaceScene:)];//selector:@selector(メソッド名:)でメソッドを指定。
CCMenuItemFont *item2 = [CCMenuItemFont itemWithString: @"replaceScene w/transition" target:self selector:@selector(onReplaceSceneTran:)];
CCMenuItemFont *item3 = [CCMenuItemFont itemWithString: @"Go Back" target:self selector:@selector(onGoBack:)];
CCMenu *menu = [CCMenu menuWithItems: item1, item2, item3, nil];
[menu alignItemsVertically];//縦に並べると指定。
[self addChild: menu];//addChild
[self schedule:@selector(testDealloc:)];//testDeallocをframeごとに呼び出し
CGSize s = [CCDirector sharedDirector].winSize;CCSprite *sprite = [CCSprite spriteWithFile:@"grossini.png"];
[self addChild:sprite];
sprite.position = ccp(40, s.height/2);
id rotate = [CCRotateBy actionWithDuration:2 angle:360];id repeat = [CCRepeatForever actionWithAction:rotate];
[sprite runAction:repeat];
}
return self;}
-(void) dealloc
{
NSLog(@"Layer2 - dealloc");[super dealloc];}
-(void) testDealloc:(ccTime) dt
{
NSLog(@"Layer2:testDealloc");
timeCounter += dt;if( timeCounter > 10 )[self onReplaceScene:self];}
-(void) onGoBack:(id) sender
{
[[CCDirector sharedDirector] popScene];}
-(void) onReplaceScene:(id) sender
{
CCScene *scene = [CCScene node];//ただのreplace用のsceneを準備
[scene addChild: [Layer3 node] z:0];//addChildeでLayer3を追加。
[[CCDirector sharedDirector] replaceScene: scene];//replaceScene}
-(void) onReplaceSceneTran:(id) sender
{
CCScene *s = [CCScene node];//あたらしいscene sを生成して、[s addChild: [Layer3 node] z:0];//addChildでLayer3を追加。
[[CCDirector sharedDirector] replaceScene: [CCTransitionFlipX transitionWithDuration:2 scene:s]];//CCTransitionFlipXtransitionWithDurationでtransitionを実装}
@end
#pragma mark -
#pragma mark Layer3
@implementation Layer3-(id) init
{
if( (self=[super initWithColor: ccc4(0,0,255,255)]) ) {
CGSize s = [[CCDirector sharedDirector] winSize];
CCMenuItemFont *item0 = [CCMenuItemFont itemWithString:@"Touch to pushScene (self)" block:^(id sender) {
CCScene *new = [CCScene node];//CCSceneを新しく生成して、Layer3をaddChild
[new addChild:[Layer3 node]];
[[CCDirector sharedDirector] pushScene:[CCTransitionFade transitionWithDuration:0.5 scene:new withColor:ccc3(0,255,255)] ];}];//scene:newをpush
CCMenuItemFont *item1 = [CCMenuItemFont itemWithString:@"Touch to popScene" block:^(id sender) {
[[CCDirector sharedDirector] popScene];}];
CCMenuItemFont *item2 = [CCMenuItemFont itemWithString:@"Touch to popToRootScene" block:^(id sender) {
[[CCDirector sharedDirector] popToRootScene];//これで、rootSceneに戻すか。パッと切り替わる。便利。transitionは無し。}];
CCMenu *menu = [CCMenu menuWithItems:item0, item1, item2, nil ];
[self addChild:menu];
[menu alignItemsVertically];//縦に並べ
[self schedule:@selector(testDealloc:)];
CCSprite *sprite = [CCSprite spriteWithFile:@"grossini.png"];
[self addChild:sprite];
sprite.position = ccp(s.width/2, 40);
id rotate = [CCRotateBy actionWithDuration:2 angle:360];id repeat = [CCRepeatForever actionWithAction:rotate];
[sprite runAction:repeat];
}
return self;}
- (void) dealloc
{
NSLog(@"Layer3 - dealloc");[super dealloc];}
-(void) testDealloc:(ccTime)dt
{
NSLog(@"Layer3:testDealloc");}
@end

#pragma mark - AppController - iOS
#if defined(__CC_PLATFORM_IOS)
@implementation AppController
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[super application:application didFinishLaunchingWithOptions:launchOptions];
// Enables High Res mode (Retina Display) on iPhone 4 and maintains low res on all other devices
if( ! [director_ enableRetinaDisplay:YES] )CCLOG(@"Retina Display Not supported");
// Turn on display FPS
[director_ setDisplayStats:YES];

// Default texture format for PNG/BMP/TIFF/JPEG/GIF images
// It can be RGBA8888, RGBA4444, RGB5_A1, RGB565
// You can change anytime.
[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];
// If the 1st suffix is not found, then the fallback suffixes are going to used. If none is found, it will try with the name without suffix.
// On iPad HD  : "-ipadhd", "-ipad",  "-hd"
// On iPad     : "-ipad", "-hd"
// On iPhone HD: "-hd"
CCFileUtils *sharedFileUtils = [CCFileUtils sharedFileUtils];
[sharedFileUtils setEnableFallbackSuffixes:YES]; // Default: NO. No fallback suffixes are going to be used
[sharedFileUtils setiPhoneRetinaDisplaySuffix:@"-hd"]; // Default on iPhone RetinaDisplay is "-hd"
[sharedFileUtils setiPadSuffix:@"-ipad"]; // Default on iPad is "ipad"
[sharedFileUtils setiPadRetinaDisplaySuffix:@"-ipadhd"]; // Default on iPad RetinaDisplay is "-ipadhd"
CCScene *scene = [CCScene node];
[scene addChild: [Layer1 node] z:0];
[director_ pushScene: scene];
return YES;}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);}
@end
#pragma mark - AppController - Mac
#elif defined(__CC_PLATFORM_MAC)
@implementation AppController
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[super applicationDidFinishLaunching:aNotification];
CCScene *scene = [CCScene node];
[scene addChild: [Layer1 node] z:0];
[director_ runWithScene:scene];
}
@end
#endif

0 件のコメント:

コメントを投稿