cocos2d-x 3.0 帧播放 (帧动画循环播放)

来源:互联网 发布:淘宝助理怎么添加分类 编辑:程序博客网 时间:2024/05/18 02:28

nb原创, 欢迎转载,转载请在明显处注明! Thx~

原文地址:http://blog.csdn.net/nilreb_nb/article/details/17161339


之前都是用2.0开发,用2.0使用帧播放需要用到CCMutableArray这个容器数组,但是3.0里面已经没有这个数组了,所以我去查看了testCpp里面的Animation源码,这边说一下3.0要怎么实现帧播放。

    

直接上代码了

(1)手动实现帧播

auto animation = Animation::create(); char str[64] = {0};for(int i=0;i<18;i++){//我一共有18张图片,放在test1文件夹sprintf(str,"test1/%d.png",i+1);animation->addSpriteFrameWithFileName(str);}animation->setDelayPerUnit(3.0f / 18.0f);//这里表示一共播放3秒,然后18张图片        animation->setRestoreOriginalFrame(true);auto action = Animate::create(animation);auto run1 = CCSprite::create();//创建一个sprite让它跑起来run1->setPosition(ccp(contsize.width/2,contsize.height/10*6));this->addChild(run1,12);        run1->runAction(Sequence::create(action, NULL));
auto animation = Animation::create();        Animation* animation = Animation::create();

 这两种定义是一个意思,我习惯用auto,方便省事。


另外如果想实现循环播放,也简单,用CCRepeat(有限)和CCRepeatForever(无限循环)

auto Call = CCCallFuncND::create(this,callfuncND_selector(CombatSystemLayer::repeatFunc),pdata);auto run = CCSprite::create();this->addChild(run,1);auto seq3 = Sequence::create(Call,DelayTime::create(8.0f),NULL);//一组动作序列的集合//CCRepeatForever* repeat=CCRepeatForever::create(seq1);//无限循环auto rep2 = CCRepeat::create(seq3,5);//把序列seq3循环播放5次auto seq4 = Sequence::create(DelayTime::create(3.5f), rep2, NULL);


(2)文件实现帧播放(这是testCpp的源码)

AnimationCache *cache = AnimationCache::getInstance();    cache->addAnimationsWithFile("animations/animations-2.plist");//加载plist文件    Animation *animation2 = cache->animationByName("dance_1");    Animate* action2 = Animate::create(animation2);    _tamara->runAction(Sequence::create(action2, action2->reverse(), NULL));


	
				
		
原创粉丝点击