cocos2d-x 创建帧动画

看到好多人问如何用cocos2d-x创建帧动画,其实用cocos2d-x很容易创建帧动画。我就写一遍吧。

void MyClass::initMyAnim()
{
    /**
    //可以在程序载入的时候预加载这些动画资源,然后在cache中读取
    CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache();
    cache->addSpriteFramesWithFile("run.plist", "run.png");
    */
    CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache();
    CCMutableArray<CCSpriteFrame*>* animFrames = new CCMutableArray<CCSpriteFrame*>(12);
    
    char str[64] = {0};
    for(int i = 1; i <=12; i++) 
    {
        sprintf(str, "run%04d.png", i);
        CCSpriteFrame* frame = cache->spriteFrameByName( str );
        animFrames->addObject(frame);
    }
    
    CCAnimation* animation = CCAnimation::animationWithFrames(animFrames,0.4f);
    CCActionInterval* action=CCAnimate::actionWithAnimation(animation,true);
CCFiniteTimeAction *myRun= CCSequence::actions(action,CCCallFunc::actionWithTarget(this,callfunc_selector(MyClass::callBackRun)),NULL);
    myRun->retain();
    animFrames->release();
}



CCCallFunc用来做动画回调用,没有回调的话,就不用了。myRun->retain()后记得release掉。

OK,是不是很简单。记录一笔。

你可能感兴趣的:(cache,null,animation,action)