cocos2dx --- 笔记 容易弄混了四个类,CCSpriteFrame CCAnimatio
先来个总结吧。 CCSpriteFrame, 是精灵的单帧信息,内部储存了精灵的Rect和贴图(贴图仅仅是保存了指针)
实现2D精灵的帧动画,遵循以下操作步骤即可。 1、首先将所有的精灵帧读取进来,然后为每一帧创建一个SpriteFrame。 2、然后,创建一个CCAnimation,接着把所有的SpriteFrame按顺序添加进来。 这里如果使用了第一帧初始化,主角精灵的话,为了保证动画播放完成,需要调用一下setRestoreOriginalFrame函数 这个函数,设置当动画播放完成以后,是否释放第一帧,true为不释放。 3、然后,动画创建完成后,如果动画需要多次播放,可以加入到AnimationCache当中, 形式如下,第一个参数是CCAnimation,第二个是动画名。 CCAnimationCache::sharedAnimationCache()->addAnimation( pAnimation,"man" ); 4、最后,在需要创建动画的地方,创建一个CCAnimate,然后把刚才创建好的CCAnimation传进去,再设置一下参数,就大功告成了。 接下来只需要选择合适的动画,再调用主角精灵的runAction就大功告成了。
示例代码: CCTexture2D *pTex = CCTextureCache::sharedTextureCache()->addImage( "man.png" ); CCSpriteFrame *frame0 = CCSpriteFrame::createWithTexture( pTex,CCRectMake(80*0,80*0,80,80)); CCSpriteFrame *frame1 = CCSpriteFrame::createWithTexture( pTex,CCRectMake(80*1,80)); CCSpriteFrame *frame2 = CCSpriteFrame::createWithTexture( pTex,CCRectMake(80*2,80)); CCSpriteFrame *frame3 = CCSpriteFrame::createWithTexture( pTex,CCRectMake(80*3,80)); CCSpriteFrame *frame4 = CCSpriteFrame::createWithTexture( pTex,CCRectMake(80*4,80)); initWithSpriteFrame( frame0 ); CCAnimation *pAnimation = CCAnimation::create(); pAnimation->setDelayPerUnit( 0.05f ); pAnimation->addSpriteFrame( frame0 ); pAnimation->addSpriteFrame( frame1 ); pAnimation->addSpriteFrame( frame2 ); pAnimation->addSpriteFrame( frame3 ); pAnimation->addSpriteFrame( frame4 ); CCAnimate *pAnimate = CCAnimate::create( CCAnimationCache::sharedAnimationCache()->animationByName( "man" ) ); CCActionInterval *pInterval; pInterval = CCSequence::create( CCScaleTo::create( 0.1f,2.0f ),pAnimate,CCScaleTo::create( 0.1f,1.0f ),CCFlipX::create( false ),NULL ); this->runAction( pInterval ); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |