iPhoneアプリ開発/cocos2d
Objective-Cの勉強も一通り終了し、今週から実際にアプリを開発する日々が始まりました。製作中のiPhoneアプリの詳細はお教えできませんが、「cocos2d」というフレームワークを使用しています。
先週のエントリーでは、今週も「Objective-Cのまとめ」の続きを更新するとお伝えしていたのですが・・・
急遽予定を変更して、自分の備忘録&復習を兼ねて「cocos2d」の簡単なまとめ(現状の)をUpしたいと思います。
「Objective-C 編」同様、まだまだ理解できていないところが諸々ありますので、進捗があり次第、編集&Upしていきます。
■「cocos2d」のインストール方法
http://www.cocos2d-iphone.org/より、
「cocos2d-iphone-0.99.5-beta3.tar.gz」(最新版)をDownload。
その後、macのターミナルで、install_template.sh を実行。
(以下、DownloadsディレクトリにDownloadした場合)
$ cd ~/Downloads
$ tar xzvf cocos2d-iphone-0.99.5-beta3.tar.gz
$ cd cocos2d-iphone-0.99.5-beta3
$ ./install_template.sh
*追記(11/16)------------------------------------------------------------------
新たなバージョン(Retina対応版)を入れようとしたときに、上記を実行したら、
cocos2d-iphone template installer
Error: This script must be run as root in order to copy templates to /Library/Application Support/Developer/Shared/Xcode
Try running it with 'sudo', or with '-u' to install it only you:
sudo ./install-templates.sh
or:
./install-templates.sh -u
というエラーがでてきてしまったので、
$ sudo ./install_template.sh
とsudoを付けるといいみたいです。(Passwordを求められます。)
詳しくは↓
http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:lesson_1._install_test
また、
In case you get errors, go to Projects→ Edit Project Settings → Build and make sure C/C+ Compiler Version is “GCC 4.2” and not LLVM GCC 4.2 or some other.
とかいてあたので、「GCC4.2」に変換。
---------------------------------------------------------------------------------------------------
installが完了後、Xcodeを起動すると、「新規プロジェクト」>「User Templates」>cocos2d 0.99.5 を選択することが可能に。
■「cocos2d」画面構成(UIViewクラス UIViewControllerクラスとの比較)
まず、復習として、「Hello World!」と画面に表示されるアプリを、「IntterfaceBuilder」を使わずに作成してみます。
Xcodeで、「新規プロジェクト」→「View-Based-Application」を選択し、新規プロジェクトを作成。
以下、「Hello World!」アプリの「Classes」フォルダ、「ViewController.m」ファイル内の「ViewDidLoad」メソッドの記述です↓
@implementation HelloWorldViewConttoller - (void) viewDidLoad { [super viewDidLoad]; UILavel *label = [UILabel alloc] initWIthFrame:CGRectMake(20,0,100.21)] [label setText:@"Hello World"]; [self.view addSubview:label]; [label release]; }
①UIViewクラスのサブクラスであるUILabelクラスから、labelインスタンスであるを作成
②UIViewクラスのインスタンスメソッドである「addSubview:label」メソッドを用いて、self(ViewControllerが管理しているView内)にlabelを表示させる。
上記の内容と、cocos2dで非常に大切な概念である
は非常に似ていました。
例として、「新規プロジェクト」→「cocos2d Application」を選択し、「Hello World!」というサンプルアプリを作成してみます。
Classesフォルダは↓のようになっています。
その中の、「HelloWorldScene」クラスファイルの実装部を見てみると、
#import "HelloWorldScene.h" // HelloWorld implementation @implementation HelloWorld +(id) scene { // シーンのインスタンスを作成 CCScene *scene = [CCScene node]; // レイヤーのインスタンスを作成 HelloWorld *layer = [HelloWorld node]; // 作成したシーンにレイヤーを追加 [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] )) { // ラベルのインスタンスを作成 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 ); // レイヤーにラベルを追加 [self addChild: label];</span> } return self; }
のようなコードになっています。
実行すると、
と表示されます。
というわけで、先ほど紹介した、UIViewクラスと、それを管理するUIViewControllerクラス。
という構造と近いものがあるなというのが率直な感想です。
cocos2dでは、UIViewControllerにあたるものが、Sceneクラス。
また、Sceneは、Layer構造になっており、各Layerに、Sprite(画像)や、Labelが配置されているイメージ。
そして、各Sceneを管理するのが、Directorクラス。
このような構造になっているようです。
図にしてみると、↓な感じです。
ちなみに、Directorクラスは、シングルトンオブジェクトのため、cocos2dフレームワークを用いたコード内には、「shared~」というメソッドを用い、シングルトンオブジェクトを呼び出す手法が頻繁に出てきます。
CCDirector *director = [CCDirector sharedDirector];
とりあえず今週はこんな感じです。
☆今週のおまけ
土日で久々に自炊というやつをしてみました。