ufolr原创,转载请注明:
转载自ufolr的博客 原文连接:http://blog.csdn.net/ufolr/article/details/7404403
前一篇教程,把项目中道具系统用到的很多东全揉搓在一起,要说明的很多,但东西实在太多,没办法一一道来。
现在我们就单独的来看看coco2d中动画的的基本实现。
利用上一篇教程中“定时炸弹”的动画来说明吧,先上资源图:
然后上代码:
bool Daoju::ZhadanAnimate(float zx, float zy){ CCLog("Animate"); CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage("Zhadan.png"); //加载资源(图片起点_左上角,图片大小) //读取资源中的每一帧,暂存到fram CCSpriteFrame *frame0 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(16*0, 16*0, 16, 16)); CCSpriteFrame *frame1 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(16*1, 16*0, 16, 16)); CCSpriteFrame *frame2 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(16*0, 16*1, 16, 16)); CCSpriteFrame *frame3 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(16*1, 16*1, 16, 16)); CCSpriteFrame *frame4 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(16*0, 16*2, 16, 16)); CCSpriteFrame *frame5 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(16*1, 16*2, 16, 16)); CCSpriteFrame *frame6 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(16*0, 16*3, 16, 16)); CCSpriteFrame *frame7 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(16*1, 16*3, 16, 16)); //设置初始图片(第一帧) CCSprite* spZhadananim = CCSprite::spriteWithSpriteFrame(frame0); spZhadananim->setPosition(ccp(zx, zy)); addChild(spZhadananim, 0, 200);//加到相应图层,设置标签为200,我们在回调函数中会利用标签找到对应的精灵,然后删除动画精灵 //合成动画-炸弹等待 CCMutableArray<CCSpriteFrame*> *animZhadanWaitFrames = new CCMutableArray<CCSpriteFrame*>(4); //animZhadanWaitFrames->addObject(frame0); //animZhadanWaitFrames->addObject(frame1); //animZhadanWaitFrames->addObject(frame2); //animZhadanWaitFrames->addObject(frame3); animZhadanWaitFrames->addObject(frame4); animZhadanWaitFrames->addObject(frame5); animZhadanWaitFrames->addObject(frame6); animZhadanWaitFrames->addObject(frame7); //合成动画-炸弹爆炸 CCMutableArray<CCSpriteFrame*> *animZhadanExciteFrames = new CCMutableArray<CCSpriteFrame*>(4); animZhadanExciteFrames->addObject(frame0); animZhadanExciteFrames->addObject(frame1); animZhadanExciteFrames->addObject(frame2); animZhadanExciteFrames->addObject(frame3); //将4个动画侦生成CCAnimation对象,每0.1秒(float)播放一帧 //生成炸弹等待动画 CCAnimation *animationWait = CCAnimation::animationWithFrames(animZhadanWaitFrames, 0.1f); CCAnimate *animateWait = CCAnimate::actionWithAnimation(animationWait, false);//动画模版生成动画 //生成炸弹激发动画 CCAnimation *animationExcit = CCAnimation::animationWithFrames(animZhadanExciteFrames, 0.2f); CCAnimate *animateExcit = CCAnimate::actionWithAnimation(animationExcit, false);//此处值为false动画播放完是在最后一帧,为true则返回第一帧 //创建回调Action CCCallFunc *animOverCallBack = CCCallFunc::actionWithTarget(this, callfunc_selector(Daoju::animBombsOverCallBack)); //依次执行Action spZhadananim->runAction(CCSequence::actions( CCRepeat::actionWithAction( animateWait, 12),//第一段动画重复12次 //CCDelayTime::actionWithDuration(0.001f), //暂停瞬间一般情况下不用 CCRepeat::actionWithAction( animateExcit, 1), //第二段动画重复1次 animOverCallBack,//回调函数,用于清屏,清除动画,否则动画播放玩后屏幕会残留在屏幕上(对应上面的值可能是最后一帧也可能是第一帧) NULL )); animZhadanWaitFrames->release();//释放资源 animZhadanExciteFrames->release(); return true; }
未完待续。。