Cocos2d-x 一边执行动作序列一边播放动画

来源:互联网 发布:淘宝孕妇装 编辑:程序博客网 时间:2024/06/11 06:22

直接上代码:

AnimLayer.h

////  AnimLayer.h//  Demo////  Created by KodeLove on 9/15/13.////#ifndef __Demo__AnimLayer__#define __Demo__AnimLayer__#include "cocos2d.h"USING_NS_CC;class AnimLayer : public CCLayer {private:        virtual bool init();    public:        CREATE_FUNC(AnimLayer);        SCENE_FUNC(AnimLayer)};#endif /* defined(__Demo__AnimLayer__) */

AnimLayer.cpp

////  AnimLayer.cpp//  Demo////  Created by KodeLove on 9/15/13.////#include "AnimLayer.h"bool AnimLayer::init() {        if (!CCLayer::init()) {        return false;    }        // 获取屏幕尺寸    CCSize t_oSize = CCDirector::sharedDirector()->getWinSize();        // -------------------- 背景 --------------------        CCLayerColor* t_pLyrColor = CCLayerColor::create(ccc4(128, 128, 128, 255));    t_pLyrColor->ignoreAnchorPointForPosition(false);    t_pLyrColor->setPosition(ccp(t_oSize.width * 0.5f, t_oSize.height * 0.5f));    this->addChild(t_pLyrColor);        // -------------------- 动画 --------------------        // 加载纹理及动画相关数据文件    CCSpriteFrameCache* t_pFrameCache = CCSpriteFrameCache::sharedSpriteFrameCache();    t_pFrameCache->addSpriteFramesWithFile("Test/bird_fly.plist");        // 将动画帧保存到数组中,为后续创建 CCAnimation 对象做准备    CCArray* t_pCcArrFrames = CCArray::create();    for (int i = 0; i < 4; i ++) {        CCString* tmp_pCcStrName = CCString::createWithFormat("bird_fly%d.png", i);        const char* tmp_pArrCharName = tmp_pCcStrName->getCString();        CCSpriteFrame* tmp_pSpriteFrame = t_pFrameCache->spriteFrameByName(tmp_pArrCharName);        t_pCcArrFrames->addObject(tmp_pSpriteFrame);    }        // 创建 CCAnimation 对象    CCAnimation* t_pAnimation = CCAnimation::createWithSpriteFrames(t_pCcArrFrames);    // 设置动画播放的时间间隔(这一步不能省略,否则动画播放不出来)    t_pAnimation->setDelayPerUnit(0.25f);        CCAnimate* t_pAnimate = CCAnimate::create(t_pAnimation);        // 让动画一遍又一遍地重复播放    CCRepeatForever* t_pRepeatForever = CCRepeatForever::create(t_pAnimate);        // -------------------- 动作序列 --------------------        CCArray* t_pCcArrActions = CCArray::create();    CCPoint t_oPntPosi;    CCMoveTo* t_pMoveTo;        // 起始点    t_oPntPosi.setPoint(t_oSize.width * 0.2f, t_oSize.height * 0.2f);    t_pMoveTo = CCMoveTo::create(0.f, t_oPntPosi);    t_pCcArrActions->addObject(t_pMoveTo);        // 左下 -> 右下    t_oPntPosi.setPoint(t_oSize.width * 0.8f, t_oSize.height * 0.2f);    t_pMoveTo = CCMoveTo::create(1.f, t_oPntPosi);    t_pCcArrActions->addObject(t_pMoveTo);        // 右下 -> 右上    t_oPntPosi.setPoint(t_oSize.width * 0.8f, t_oSize.height * 0.8f);    t_pMoveTo = CCMoveTo::create(1.f, t_oPntPosi);    t_pCcArrActions->addObject(t_pMoveTo);        // 右上 -> 左上    t_oPntPosi.setPoint(t_oSize.width * 0.2f, t_oSize.height * 0.8f);    t_pMoveTo = CCMoveTo::create(1.f, t_oPntPosi);    t_pCcArrActions->addObject(t_pMoveTo);        // 左上 -> 左下    t_oPntPosi.setPoint(t_oSize.width * 0.2f, t_oSize.height * 0.2f);    t_pMoveTo = CCMoveTo::create(1.f, t_oPntPosi);    t_pCcArrActions->addObject(t_pMoveTo);        // 创建动作序列    CCSequence* t_pSequence = CCSequence::create(t_pCcArrActions);        // -------------------- 开动了 --------------------        /**     * 这里用不上 CCSpawn,调用两次 runAction 就行了     * 实际上,如果用 CCSpawn 将 repeat 和 sequence 复合起来的话,反而只会有 sequence 生效,     * 至于 repeat 的动画的话,是播放不出来的!参考链接:     * http://bluthmatter.blog.163.com/blog/static/184294059201301594334708/     */    CCSprite* t_pSpActor = CCSprite::createWithSpriteFrameName("bird_fly0.png");    t_pSpActor->runAction(t_pRepeatForever);    t_pSpActor->runAction(t_pSequence);    this->addChild(t_pSpActor);        return true;}

原创粉丝点击