cocos2d animation アニメーション

よくドラクエを代表とするRPGでキャラクタが歩いている動作を表現するためにアニメーションが必要です。 今回は、その様な動きがcocos2dでどうやったらいいのかを調べました。

今回もRAYWENDERLICHチュートリアルを参考にしました。

まずはアニメーションのもとになる画像を用意します。

これにはZwoptexというツールを使っています、 Zwoptexを使って、動きのアニメーションの画像を一つの画像にまとめることができます。

これで、cocos2dで使う際にはまずplist と画像のファイルを読み込みます。

[cpp]

// plist を読み込む [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile: @"Gazou.plist"];

// 一つにまとめた画像のデータを読み込む CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"Gazou.png"];

//読み込んだデータをシーンに追加する [self addChild:spriteSheet];

//配列を使って、一つにまとめた画像から個別の画像を取り出す。 NSMutableArray *animFrameArray = [NSMutableArray array]; for(int i = 1; i <= 6; ++i) { animFrameArray addObject: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:@"Gazou%d.png", i]]]; }

[/cpp]

詳しくはわからないですが、CCSpriteBatchNodeを使った方が早いみたいです。

CCAnimation

アニメーションで使う画像をCCAnmationを使って作成します。 [cpp]

CCAnmation *animeFrames = [CCAnimation animationWithFrames:画像の配列                              delay:0.1f];

[/cpp]

これをCCActionの一つであるCCAnimateで利用をする。

[cpp]

CCAction *animAction = [CCAnimate actionWithAnimation:animeFrames restoreOriginalFrame:NO];

[player runAction:animAction];

[/cpp]

その他

flipX,flipY

左右、上下反転して画像を表示させる。

タッチ操作に反応する処理を書く際にはまず以下のコードを書く必要があるみたいです。 [cpp] -(void) registerWithTouchDispatcher{ [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES]; }

[/cpp]