cocos2dの使い方【説明】① 〜ここs2d って何?笑〜

前回作ったHelloWorldプロジェクトを見ていきましょう。

今回の説明項目は以下の通りです。

・DirectorがSceneを、SceneがLayerを、LayerがLabel、Spriteを管理する
・Sceneの生成は[CCScene node]
・SceneがLayerを登録するのは[scene addChild: layer]
・Layerの生成は[CCLayer node]
・LayerがLabel、Spriteを登録するのは[layer addChild:label]

まずcocos2dの仕組みを簡単に説明していきます。


これからゲームをcocos2dで作っていく訳ですが,ゲーム自体はDirectorが管理しています。


DirectorはSceneを管理しています。

例えば

・タイトル画面
・ゲーム選択画面
・ゲーム画面
・設定画面 など,

言葉の通り場面ごとに作っていくものです。


そしてSceneはLayerを管理しています。

例えば



図のように例えばタイトル画面(Scene)にも

・背景レイヤー
・アニメーションレイヤー
・メニューレイヤー など,

があり,Layerを重ねて画面を作っています。


そしてLayerはLabel,Spriteなどを管理します。

例えば

・背景レイヤー(背景画像(Sprite))
・アニメーションレイヤー(キャラクタ画像(Sprite), アイテム画像(Sprite))
・メニューレイヤー(メニューの文字(Label)) など,

それぞれのレイヤーで必要な画像(Sprite)や文字(Label)などを管理します。


ではHelloworldプロジェクトを見てみます。

HelloWorldグループにある,AppDelegate.m を開く。

すると,


ー (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions というメソッドの中の一番下に [director_ pushScene: [HelloWorldLayer scene]]; というのがあると思います。

ー (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
                    .
                    .
                    .


	[director_ pushScene: [HelloWorldLayer scene]];  ←コレ!

	return YES;
}

ここでSceneを呼ぶ事でゲームは始まっています。

ちなみに
[クラス名 動的メソッド];
[クラスポインタ 静的メソッド];
でそれぞれのメソッドを呼び出せます。
(詳しくはhttp://wisdom.sakura.ne.jp/programming/objc/index.htmlなどで簡単に勉強できます。)

そこでこのsceneはどこにあるのかというと,HelloWorldLayerクラスにあるはずですので

HelloWorldLayer.h を開くと

#import <GameKit/GameKit.h>

// When you import this file, you import all the cocos2d classes
#import "cocos2d.h"

// HelloWorldLayer
@interface HelloWorldLayer : CCLayer  ←②
<GKAchievementViewControllerDelegate, GKLeaderboardViewControllerDelegate>
{
}

// returns a CCScene that contains the HelloWorldLayer as the only child

+(CCScene *) scene; ←①

@end

①に +(CCScene *) scene; があり,さっきのはこれを呼んでいることになります。

そこで,これはヘッダーファイルですので+(CCScene *) sceneがメソッドファイル(.m)に書かれていることを示します。

そこで HelloWorldLayer.m を開くと

+(CCScene *) scene
{
	// 'scene' is an autorelease object.
	CCScene *scene = [CCScene node];
	
	// 'layer' is an autorelease object.
	HelloWorldLayer *layer = [HelloWorldLayer node];
	
	// add layer as a child to scene
	[scene addChild: layer];
	
	// return the scene
	return scene;
}

と書かれているのがわかると思います。

ここではまずCCSceneのポインタsceneを作り[CCScene node]; でsceneというSceneを作った事になります。

次にそのシーンに貼るLayerも作っていますが,ここでは型がHelloWorldLayerになっています。

ここれはこれはCCLayerに当たります。もう一度ヘッダファイル(HelloWorldLayer.h)を見てみると,②の部分
でこのクラスはCCLayer型として作られていた事がわかります。

CCLayerで作らず,そのクラス名で作る事により自分(HelloWorldLayerクラスの本体)はこれだということになります。

そしてHelloWorldLayer(CCLayer)のポインタlayerを作り[HelloWorldLayer node]; でlayerというLayerを作ったことになります。

そして先ほど作ったsceneにlayerを [scene addChild: layer]; で貼り,

この今作ったsceneを返す事で,このsceneを表示します。

まだ,LayerにはなにもないのでHelloWorldという文字を表示するため,Labelを生成します。

-(id) init
{
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super's" return value
	if( (self=[super init])) {
		
		// create and initialize a Label
		CCLabelTTF *label = [CCLabelTTF labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64];  ←①

		// ask director the the window size
		CGSize size = [[CCDirector sharedDirector] winSize];
	
		// position the label on the center of the screen
		label.position =  ccp( size.width /2 , size.height/2 ); ←③
		
		// add the label as a child to this Layer
		[self addChild: label]; ←②
		
                .
                .
                .
}

①で生成し,②でレイヤーに貼付けています。③ではLabelの位置を指定しています。

これで,HelloWorldという文字が書かれているLabelをもつLayerをもつSceneが表示され,起動した通りになります。



次回も中身の説明をして実際にいじってみましょう。