cocos2dにてテクスチャアトラスを使用してアニメーションをさせる方法
ひっさしぶりにプログラムのうんちく。。。。。
Ccos2Dでテクスチャアトラスを使用してアニメーションをする方法を書いておきます。
事前に、TexturePacker」などでテクスチャアトラスのデータは作成しておいてください。
以下の関数をCCSpriteクラスを継承したクラスに貼り付けて、
クラスインスタンスをしたあとにこの関数を実行する。
-(void) animationLoop:(NSString *)_plist // テクスチャアトラスのplistデータ
paramName:(NSString *)_paramName // ファイル名フォーマット(例:test%d04i.png)
itemMaxNumber:(int)_itemMaxNumber // アニメーションの駒数
{
//テクスチャーをキャッシュするためのまじない
CCSpriteFrameCache* frameCache = [CCSpriteFrameCachesharedSpriteFrameCache];
[frameCache addSpriteFramesWithFile:_plist];
NSMutableArray* frames = [NSMutableArrayarrayWithCapacity:5];
for (int i=0; i< _itemMaxNumber; i++) {
NSString* file = [NSString stringWithFormat:_paramName,i+1];
CCSpriteFrame* frame = [frameCache spriteFrameByName:file];
[frames addObject:frame];
}
// アニメーションフレームをアニメーションにしてスプライトと紐付ける
CCAnimation* anim = [[[CCAnimation alloc] initWithSpriteFrames:frames delay:0.05f] autorelease];
CCAnimate* animate = [CCAnimate actionWithAnimation:anim];
CCRepeatForever* repeat = [CCRepeatForeveractionWithAction:animate];
[self runAction:repeat];
}
いらなくなったときは、「removeFromParentAndCleanup」を実施する。
例:
[ssspriteClassIns removeFromParentAndCleanup:YES];
こんな感じですがいいですかね。。。。。(笑)
一応、Ver.2.0で動作します。
Ver.1.xでは、メモリ解放がうまく動作しません。
解放されているように見えるのですが、メモリを食ったままでした。
なぜなんでしょうかね。。。。(・_・?)ワカラン