C++初心者が【cocos2d-x】を勉強する。

cocos2d-xを勉強する際に、勉強をしたことを書いていきます。 C++は全く触ったことない、objective-cはちょっと分かる程度のプログラミング知識しかないですが、iPhoneAndroidのアプリを同時に作ることができるというcocos2d-xを覚えたい。

C++の本が、手元に無く、ネットでも余り初心者向けのサイトがないので、自力で勉強をしていきたいと思います。

デンプレートを比較して、C++の構文を見る

cocos2dとcocos2d-xのデンプレートプロジェクトHelloWorldを比較して、その違いでC++がどういったものなのかを見てみます。

コードの比較

cocos2d-iPhone

[c] +(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;

} [/c]

cocos2d-x

[c] CCScene HelloWorld::scene() { // 'scene' is an autorelease object CCScene scene = CCScene::create();

// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create();

// add layer as a child to scene
scene->addChild(layer);

// return the scene
return scene;

}

[/c]

コードの比較と考察

メソッドの定義。

[c] CCScene*HellowWorld::scene(){

} [/c]

CCSceneが戻り値の型 HellowWorldがファイルの名前 ::scene()がクラスメソッド

クラスメソッドを利用する

[c] CCScene *scene = CCScene::create(); [/c]

おそらく、クラスにあるクラスメソッドを利用する際には[ クラス名 :: メソッド ]の形にするようだ。 クラスメソッドのcreate()は、cocos2d-iPhoneで入れば[CCScene node]のような感じで初期化などをするメソッドなのだろう。

インスタンスメソッドを利用数

[c] scene -> addChild(layer); [/c]

先ほどのクラスメソッドの場合は[ クラス名 :: メソッド ]のように利用していると考えられるのに対して、インスタンスメソッドは[初期化されたオブジェクト -> インスタンスメソッド名(引数)]のような形になるようだ。 なんだかメンドクサイ感じがする。

HelloWorldの出力を比較する

次は実際にHelloWorldを文字として出力している箇所を見ていきます。

[c] //cocos2d-iPhone

// create and initialize a Label CCLabelTTF *label = [CCLabelTTF labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64];

// ask director for 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];

[/c]

[c] //cocos2d-x

// create and initialize a label
CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Thonburi", 34);

// ask director the window size
CCSize size = CCDirector::sharedDirector()->getWinSize();

// position the label on the center of the screen
pLabel->setPosition( ccp(size.width / 2, size.height - 20) );

// add the label as a child to this layer
this->addChild(pLabel, 1);

[/c]

コードの比較と考察

[c] CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Thonburi", 34); [/c]

CCLabelTTFクラスで、クラスメソッドcreate()を利用している。 引数が3つあるので、それぞれ「文字」「フォント名」「フォントサイズ」を指定しているのがわかる。 これは形は違えどcocos2dと同じです。

[c] CCSize size = CCDirector::sharedDirector()->getWinSize();

[/c] 画面のサイズを導くCCDirectorクラスも、クラスメソッドとインスタンスメソッド(getWinSize)を利用しているのがわかる。 getWinSize()が画面のサイズをCGSize型で返すメソッドなのだろう。cocos2dとメソッド名が微妙に違うんだな。

[c] pLabel->setPosition( ccp(size.width / 2, size.height - 20) );

[/c] setPosition()がインスタンスメソッドで引数にCGPoint型を指定。 これはcocos2dのものとほぼ同じ。

[c] this->addChild(pLabel, 1); [/c] 最後はlabelを。CCLayerに追加しているインスタンスメソッドaddChild。 引数の1はおそらくZ値であると推定できる。

最後に

デンプレートの比較して、少し理解ができましたが、今回見たのは本当に基本中の基本のことだと思いますので、もっとcocos2d-xのコードを読まないといけないと感じます。 まだまだ情報が少ないので、効率が悪いですが、cocod2dと比較して、勉強していくしかないのかな。

C++及びcocos2d-xで何か良い勉強の仕方があれば、教えてください!