kingBook

导航

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(){

}

 2.plist文件创建动画

 

#include "TrademarkAnimation.h"

bool TrademarkAnimation::init(){
    if(!CCLayer::init())return false;

    Size size=Director::getInstance()->getVisibleSize();
    Sprite* runSp=Sprite::create();
    runSp->setPosition(size.width*0.5,size.height*0.5);
    this->addChild(runSp);
    runSp->runAction(createAnimate());
    return true;
}

Animate* TrademarkAnimation::createAnimate(){
    //如果flash出图,设置:
    //Algorithm: Basic/MaxRects
    //Data format: cocos2D v2
    //Trim: false
    //Stack frames: false
    SpriteFrameCache* frameCache=SpriteFrameCache::getInstance();
    frameCache->addSpriteFramesWithFile("trademark.plist","trademark.png"); //或 frameCache->addSpriteFramesWithFile("trademark.plist");

    int totalFrames=319;
    SpriteFrame* frame=NULL;
    Vector<SpriteFrame*> frameVec;
    for(int i=0;i<totalFrames;i++){
        //T1 instance 10000.......T1 instance 10318
        frame=frameCache->getSpriteFrameByName(StringUtils::format("T1 instance 1%04d",i));
        frameVec.pushBack(frame);
    }

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

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

TrademarkAnimation::~TrademarkAnimation(){

}

 

posted on 2016-11-26 16:38  kingBook  阅读(418)  评论(0编辑  收藏  举报