动作-CCActionInterval之CCAnimation&CCAnimate

动画简单实例

201311182109.jpg

    pic2476.png

简单的动画代码:

CCSize winSize=CCDirector::sharedDirector()->getWinSize();         //#1:生成动画需要的数据类     CCTexture2D *texture=CCTextureCache::sharedTextureCache()->addImage(“pic2476.png”);    CCSpriteFrame *frame0=CCSpriteFrame::createWithTexture(texture,CCRectMake(32*0,48*0,32,48));    CCSpriteFrame *frame1=CCSpriteFrame::createWithTexture(texture,CCRectMake(32*1,48*0,32,48));    CCSpriteFrame *frame2=CCSpriteFrame::createWithTexture(texture,CCRectMake(32*2,48*0,32,48));    CCSpriteFrame *frame3=CCSpriteFrame::createWithTexture(texture,CCRectMake(32*3,48*0,32,48));     CCArray *arrayFrames=CCArray::create();    arrayFrames->addObject(frame0);    arrayFrames->addObject(frame1);    arrayFrames->addObject(frame2);    arrayFrames->addObject(frame3);     CCAnimation *animation=CCAnimation::createWithSpriteFrames(arrayFrames,0.2f);    //#2:初始化并设置Sprite    CCSprite *sprite=CCSprite::createWithSpriteFrame(frame0);    sprite->setPosition(ccp(winSize.width/2,winSize.height/2));    addChild(sprite);    //#3:使用animation生成一个动画动作animate     CCAnimate *animate=CCAnimate::create(animation);    sprite->runAction(CCRepeatForever::create(animate));

注意,cocos2dx不支持使用clip的动画,另外,clip动画的开发成本很高,在智能手机这种大内存的平台是否适用(牺牲内存换开发速度么?),值得商量。

#2 相关的类关系图

说明: http://img1.51cto.com/attachment/201201/122458884.png

简单过程是,使用CCTexture2D加载图片 ,用CCTexture2D生成对应的CCSpriteFrame(对应的就是帧),将CCSpriteFrame添加到CCAnimation生成动画数据,用CCAnimation生成CCAnimate(就是最终的动画动作),最后用CCSprite执行这个动作。

原文地址:https://www.cnblogs.com/yssgyw/p/3430234.html