【cocos2d入門】Hello World!

Posted by: daichi  /  Category: iphone開発

前回までのcocos2d入門。
【cocos2d入門】cocos2dの導入 | iphoneアプリで稼げるのか

前回でcocos2dの導入が終了。
今回はcocos2dでHello World!します。
ここで使うcocos2dのバージョンは、cocos2d-0.9.0-alphaです。


プロジェクト作成

新規にプロジェクトを作成します。
前回の導入で3つのcocos2dテンプレートが作成されています。
それぞれ通常のアプリ、Box2dを使うアプリ、Chipmunkを使うアプリのようです。
(多分Box2dとChipmunkは物理演算用アプリかな。)
ここでは通常のcocos2d-0.9.0-alpha Applicationを選択してプロジェクトを作成します。
プロジェクト作成

名前はcocos2dTestにしました。

早速ビルド

ビルドすると早々にHello World!が表示されるではありませんか。
Hello Worldダメ

これは反則です。
なので1から作ることにします。

Hellow World用ターゲットを作る

せっかくなので、cocos2dのソースコードのようにターゲットを指定して、複数のチュートリアルアプリを作っていこうと思います。

グループとファイル→ターゲットを右クリック→追加→新規ターゲット。
新規ターゲット

Applicationを選択。
Application

ターゲット名はHelloWorldで。

続いてターゲットの情報画面が開くので、直接依存関係にcocos2d Libraryを追加。
HelloWorldターゲット

次に、グループとファイル→Frameworks→右クリック→情報をみる
Frameworkをターゲットに追加

HelloWorldターゲットにチェックを入れる。
HelloWorldターゲットにチェックを入れる

Resourceも同様に。
Resourceをターゲットに追加

それからProductsにあるcocos2d libraries.aのターゲットにもHelloWorldを追加。
cocos2d libraries.aをHelloWorldへ

最後にClassesの中にHelloWorldグループを作っておきます。
HelloWorldグループ
HelloWorld用ファイルはここに入れることにします。

いよいよHelloWorld!

それではHelloWorldします。

HelloWorldApplicationクラスを作成します。
ターゲットはHelloWorldのみを選択!
HelloWorldApplication

このHelloWorldApplication.hと.mに今回のHelloWorldアプリの全てを書くことにしますが、その前に、

Cocos2dの考え方

prog_guide:basic_concepts [cocos2d for iPhone]

ここにcocos2dの概要が書いてありますが、cocos2dの世界では大きく
  • Scenes
  • Director
  • Layers
  • Sprites
の4つが主要な登場人物らしい。説明の中ではLabelも出てきてる。

ざっくりまとめてしまうと、
Directorが複数のSceneを管理し、Sceneが複数のLayerを管理し、Layerが複数のSprite、Labelを管理する。ということらしい。

Directorはシングルトンでアプリに1つだけ存在する。
Sceneはオープニングシーンやメニューシーン、1面、2面などを表す。

scenes

LayerはSceneの中の背景レイヤやメニューレイヤ、アニメーションレイヤなどを表す。

layers

Spriteはレイヤの中の山の画像、雲の画像を。
Labelはレイヤの中の文字。

という感じ。

なのでHelloWorldではHelloWorldを出すSceneとLayer、HelloWorldの文字を表示するLabelが必要ということになる。

今度こそHelloWorld

というわけでHelloWorldしましょう。

HelloWorldApplication.h

?View Code OBJECTIVE-C
#import
#import "cocos2d.h"
 
@interface HelloWorldApplication : NSObject  {
	UIWindow *window;
}
 
@property (nonatomic, retain) UIWindow *window;
 
@end
 
// HelloWorld Layer
@interface HelloWorldLayer : CCLayer
{
}
 
// returns a Scene that contains the HelloWorld as the only child
+(id) scene;
 
@end


HelloWorldApplication.m

?View Code OBJECTIVE-C
#import "HelloWorldApplication.h"
 
@implementation HelloWorldApplication
 
@synthesize window;
 
- (void) applicationDidFinishLaunching:(UIApplication*)application
{
	// Init the window
	window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
 
	// create an openGL view inside a window
	[[CCDirector sharedDirector] attachInView:window];
	[window makeKeyAndVisible];		
 
	[[CCDirector sharedDirector] runWithScene: [HelloWorldLayer scene]];
}
 
- (void)dealloc {
	[[CCDirector sharedDirector] release];
	[window release];
	[super dealloc];
}
 
@end
 
@implementation HelloWorldLayer
 
+(id) 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;
}
 
// on "init" you need to initialize your instance
-(id) init
{
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super" return value
	if( (self=[super init] )) {
 
		// create and initialize a Label
		CCLabel* label = [CCLabel 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];
	}
	return self;
}
 
// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
	// in case you have something to dealloc, do it in this method
	// in this particular example nothing needs to be released.
	// cocos2d will automatically release all the children (Label)
 
	// don't forget to call "super dealloc"
	[super dealloc];
}
 
@end
 
int main(int argc, char *argv[]) {
	NSAutoreleasePool *pool = [NSAutoreleasePool new];
	int retVal = UIApplicationMain(argc, argv, nil, @"HelloWorldApplication");
	[pool release];
	return retVal;
}


結局、コピペしたのはここだけの話ですが、重要なポイントをピックアップしてみます。

?View Code OBJECTIVIE-C
	[[CCDirector sharedDirector] runWithScene: [HelloWorldLayer scene]];


cocos2dのディレクターはCCDirectorクラスです。シングルトンなのでクラスメソッドのsharedDirectorで呼び出すようになってます。

また、DirectorがSceneを開始するのは、runWithSceneメソッド。
引数にSceneを渡してあげればSceneが開始されます。

?View Code OBJECTIVE-C
+(id) 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;
}


続いて、HelloWorldLayerのsceneメソッド。
cocos2dのSceneはCCSceneクラスが、LayerはCCLayerクラスが担当します。
ここでは、Sceneの管理するLayerを登録しています。
これはおそらくLayerが1つだからこういう作りにしてるのだと思いますが、LayerクラスのクラスメソッドでSceneを生成して、Layerも生成して、SceneにLayerをaddChildして登録してます。
Sceneの生成は
[CCScene node]
で。
Layerの生成は
[CCLayer node]
でできます。

?View Code OBJECTIVE-C
-(id) init
{
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super" return value
	if( (self=[super init] )) {
 
		// create and initialize a Label
		CCLabel* label = [CCLabel 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];
	}
	return self;
}


最後にLayerクラスのinitメソッド。
ここでは、Layerが管理(表示)するLabelを登録してます。
LabelはCCLabelが担当します。
この部分はUIKit系と同じ感覚でできそうです。
UIViewを作ってframeを定義して、viewにaddSubviewする感覚で、CCLabelを作って、positionを定義して、layerにaddChildすると。

ccpはcocos2dの便利マクロらしく、CGPointをMakeしてくれるようです。
labelのpositionはUIViewでいうcenterと同じっぽい。

完成はこちら。

HelloWorld!

まとめ

というわけで、駆け足で見てきましたが、
ポイントは、
  • DirectorがSceneを、SceneがLayerを、LayerがLabel、Spriteを管理する
  • DirectoreがSceneを実行するのは[CCDirector sharedDirector] runWithScene:scene]
  • SceneがLayerを登録するのは[scene addChild: layer]
  • LayerがLabel、Spriteを登録するのは[layer addChild:label]
  • Sceneの生成は[CCScene node]
  • Layerの生成は[CCLayer node]
これだけおさえておけば、最低1シーンの画面表示はできるでしょう。きっと。

さて、次回はActionについてやります。

タグ: cocos2d, チュートリアル, ライブラリ, 入門

関連する投稿

コメント

Get Adobe Flash playerPlugin by wpburn.com wordpress themes