SOHOゆいちのサイト
cocos2dのサンプルを勉強がてら読みといていくメモです。ほとんどのテストが同じ基本的な構造を持っているみたいなので、ActionManagerTestを見ながら各テストが実行される全体の流れを先ず見てみます。勉強上のメモなので記載内容に責任は、、、もし間違いがあったらどなたか指摘ください。
CC_DIRECTOR_INIT();
コメントを見ると
// CC_DIRECTOR_INIT() // // 1. Initializes an EAGLView with 0-bit depth format, and RGB565 render buffer // 2. EAGLView multiple touches: disabled // 3. creates a UIWindow, and assign it to the "window" var (it must already be declared) // 4. Parents EAGLView to the newly created window // 5. Creates Display Link Director // 5a. If it fails, it will use an NSTimer director // 6. It will try to run at 60 FPS // 7. Display FPS: NO // 8. Device orientation: Portrait // 9. Connects the director to the EAGLView
と書かれているので、
をやってくれるらしい。
CCDirector *director = [CCDirector sharedDirector];
director変数へDirectorのインスタンスを代入している。
[director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
iPhoneの画面を横表示に指定している。
[director setDisplayFPS:YES];
画面左下にFPSを表示する。
CCScene *scene = [CCScene node];
シーンを生成、CCNode.mで以下のように生成しているからreleaseも意識しなくていいっぽい
+(id) node
{
return [[[self alloc] init] autorelease];
}
[scene addChild: [nextAction() node]];
最初の方で宣言している「nextAction」クラスで生成されるクラスのnodeメソッドを呼び出している。nextActionは次に表示するシーンのクラス名を返してくれる。(詳細はもう少し下で)これをシーンにセットする。
[director runWithScene: scene];
directorで上のシーンを実行
cocos2dではなく、SDKのイベント処理関係、おまじない的にcocos2dで開発するときは書いておくイメージ。
アプリケーションがアクティブでなくなる直前に呼ばれるメソッド
[[CCDirector sharedDirector] pause];
directorへpauseを指示(これがないと、途中で電話がかかってきた時などに裏で動き続けて、ゲームだと死んでしまったりする)
applicationWillResignActiveの逆でアクティブにもどるときに呼ばれる。
[[CCDirector sharedDirector] resume];
directorをpause状態からもどす。
メモリ警告発生時のイベント、
[[CCDirector sharedDirector] purgeCachedData];
CCDirector.mを見ると、各種Casheをクリアしている。
処理速度が追いつかないときに発生するイベント
[[CCDirector sharedDirector] setNextDeltaTimeZero:YES];
nextDeltaTimeZeroはCCDirector.hでpropertyが宣言されている、http://www.cocos2d-iphone.org/forum/topic/72によるち、速度が追いつかないときに、停止して、追いついてきたら続きから動作するというものっぽい。
なぜ、ここでいきなりCなのかというのは置いておいて、
sceneIdx++; sceneIdx = sceneIdx % ( sizeof(transitions) / sizeof(transitions[0]) );
sceneIdxを1つ増やし、その数がtransitionsの要素数より大きくなったら、余剰をとって、最初に戻している。
NSString *r = transitions[sceneIdx]; Class c = NSClassFromString(r);
transitions配列からクラス名を取得しそのクラス名のクラスのオブジェクトを取得する。
sceneIdx--; int total = ( sizeof(transitions) / sizeof(transitions[0]) ); if( sceneIdx < 0 ) sceneIdx += total;
nextActionクラスとは逆で、sceneIdxを1つ減算して、その数が0より小さくなった場合は、transitions配列の一番最後のキーをsceneIdxとする。後はnextActionと同じ。
sceneIdxを変更せずに、クラスのオブジェクトを返す。
テスト処理のメインクラス、ここでテスト間の移動などをやっている。ActionManagerTestクラスはCCLayerを継承しているので、これ自身がレイアとなる。
[super init];
親のinitメソッドを呼び出す。
CGSize s = [[CCDirector sharedDirector] winSize];
画面サイズを取得
CCLabel* label = [CCLabel labelWithString:[self title] fontName:@"Arial" fontSize:32];
フォント名「arial」フォントサイズ32ptでタイトルラベルを生成。タイトルの文字列は「-(NSString*) title;」で設定されている。
[self addChild: label z:1];
生成したラベルをzindexを1として追加(zindexは各オブジェクトの重なりを示し、デフォルトは0)。
[label setPosition: ccp(s.width/2, s.height-50)];
生成したラベルの位置を左右の中央、上下は下から50pxにセットする。ccpはCGPointMakeのaliasで指定した座標のCGPointクラスを返す。
NSString *subtitle = [self subtitle];
サブタイトルを変数へセット
if( subtitle ) {
CCLabel* l = [CCLabel labelWithString:subtitle fontName:@"Thonburi" fontSize:16];
[self addChild:l z:1];
[l setPosition:ccp(s.width/2, s.height-80)];
}
サブタイトルがセットされていれば、フォント名「Thonburi」フォントサイズ16でラベルを生成し、zindex=1でレイヤにセット、位置を調整する。
※ActionManagerTestではnilを返すようになっているため実行されない。
CCMenuItemImage *item1 = [CCMenuItemImage itemFromNormalImage:@"b1.png" selectedImage:@"b2.png" target:self selector:@selector(backCallback:)];
b1.png(Resourdes/Imagesフォルダに有る)、選択時は(b2.png)でボタンを生成する。押された場合は、「backCallback」メソッドを実行する。
CCMenuItemImage *item2 = [CCMenuItemImage itemFromNormalImage:@"r1.png" selectedImage:@"r2.png" target:self selector:@selector(restartCallback:)]; CCMenuItemImage *item3 = [CCMenuItemImage itemFromNormalImage:@"f1.png" selectedImage:@"f2.png" target:self selector:@selector(nextCallback:)];
item1同様にrestartCallback、nextCallbackのボタンを生成。
CCMenu *menu = [CCMenu menuWithItems:item1, item2, item3, nil];
作成したitem1,item2,item3をメニューとしてCCMenuクラスを生成する。
menu.position = CGPointZero; item1.position = ccp( s.width/2 - 100,30); item2.position = ccp( s.width/2, 30); item3.position = ccp( s.width/2 + 100,30);
メニューのそれぞれの位置を調整。
[self addChild: menu z:1];
メニューをレイアにセットする。
CCScene *s = [CCScene node];
新しいシーンを生成する。
[s addChild: [restartAction() node]];
restartActionクラスから現在のレイヤクラスを呼び出し、生成したシーンに追加する。
[[CCDirector sharedDirector] replaceScene: s];
directoryで現在のシーンと新しく生成したシーンを差し替える。
restartCallbackメソッド同様にそれぞれnextAction,backActionから次のシーンのクラスを呼び出しシーンを差し替える。
大体どのテストもAppController、テストのレイアクラス(ここではActionManagerTest)をベースにして実行されているようです。ActionManagerTestを継承した個々のレイヤクラスが実際のテストを実行しているようなので、それぞれそこを読み解いていけば理解していけそう?です。
www.bshe.org (created by itassist.info)