Cocos2dでフレームアニメーション
いわゆるテクスチャアトラスを使ったアニメーションです。
CCSpriteSheetを使ったサンプルは見つかるのですが、既にそれは
CCSpriteBatchNodeに置き換わっているので参考になりません。
私は他のモテるblogを書いている方々とは違って
サンプル用の画像を用意したりするような気遣いが出来ませんので、
512x512のpng画像に、64x64の画像を左下から横並びに8枚並べた場合を想定して作ります。
CGSize screenSize = [CCDirector sharedDirector].winSize; // BatchNodeにテクスチャを登録し、Layerに追加します。 // effect512.pngの部分はご自分の環境に合わせて変更してください。 CCSpriteBatchNode *batch = [CCSpriteBatchNode batchNodeWithFile:@"effect512.png" capacity:1]; [self addChild:batch]; // アニメーションの最初のスプライトを読み込む CCSprite* sprite = [CCSprite spriteWithBatchNode:batch rect:CGRectMake(0, 448, 64, 64)]; sprite.position = ccp(screenSize.width/2, screenSize.height/2); [batch addChild:sprite]; // アニメーションのコマに対応するスプライトをフレームとして作成する NSMutableArray *animFrames = [NSMutableArray array]; for(int i=0; i<8; i++) { CCSpriteFrame *frame = [CCSpriteFrame frameWithTexture:batch.texture rect:CGRectMake(i*64, 448, 64, 64)]; [animFrames addObject:frame]; } // アニメーションフレームをアニメーションとしてスプライトと紐付ける // 取り合えず無限に繰り返すアニメーション CCAnimation *animation = [CCAnimation animationWithFrames:animFrames delay:0.05f]; id repeatAnim = [CCRepeatForever actionWithAction: [CCAnimate actionWithAnimation:animation restoreOriginalFrame:NO]]; [sprite runAction:repeatAnim];
目で見ながら打ち込んでるので間違いがあるかも。
適当に8枚のフレームを無限に繰り返すアニメーション。
ボールの回転とかにしてるので、速度はやめで繰り返してます。