首页 > 代码库 > cocos2d-3.x 创建动画

cocos2d-3.x 创建动画

1.多文件帧序列动画

TrademarkAnimation.h

#ifndef __TRADEMARK_ANIMATION_H__
#define __TRADEMARK_ANIMATION_H__
#include "cocos2d.h"
USING_NS_CC;

class TrademarkAnimation : public cocos2d::CCLayer{
    private:
    Animate* createAnimate();
public :
    ~TrademarkAnimation();
    virtual bool init();
    
    CREATE_FUNC(TrademarkAnimation);
};
#endif

TrademarkAnimation.cpp

#include "TrademarkAnimation.h"

bool TrademarkAnimation::init(){
    if(!CCLayer::init())return false;
    Sprite* runSp=Sprite::create();
    runSp->setPosition(Point(200,200));
    this->addChild(runSp);

    runSp->runAction(createAnimate());
    return true;
}

Animate* TrademarkAnimation::createAnimate(){
    int totalFrames=319;
    SpriteFrame* frame=NULL;
    Vector<SpriteFrame*> frameVec;

    for(int i=1;i<=totalFrames;i++){
        //tdmark/tdmark0001.png....tdmark/tdmark0319.png
        frame=SpriteFrame::create(StringUtils::format("tdmark/tdmark%04d.png",i),Rect(0,0,354,270));
        frameVec.pushBack(frame);
    }

    Animation* animation=Animation::createWithSpriteFrames(frameVec);
    animation->setLoops(-1);
    animation->setDelayPerUnit(0.04f);//: 1/帧频

    Animate* action=Animate::create(animation);
    return action;
}

TrademarkAnimation::~TrademarkAnimation(){

}

 

cocos2d-3.x 创建动画