我不需要知道你有多少帧之动画创建辅助类(转载)

原文链接:http://blog.csdn.net/musicvs/article/details/8567220

 

我不需要知道你有多少帧之动画创建辅助类

今天心情不好,所以我可能会比平时唠叨。

(小若:你心情不好,那为毛要连累我们!)

OK,今天不是写教程,只是想给大家分享一下简单的心得。想必大家都觉得Cocos2d-x要创建一个CCAnimation还是有点繁琐的,是的,通常我们都会创建一个辅助类,这样就可以说省去重复的工作了。

(小若:快点进入正题,我也没有心情吐槽。)

笨木头花心贡献,啥?花心?不呢,是用心~

转载请注明,原文地址
http://blog.csdn.net/musicvs/article/details/8567220

正文:

比如,我们通常会看到这样的辅助类:

  1. CCAnimation* AnimationUtil::createAnimationWithSingleName( const char* singleName, int iFrameNum ) {  
  2.     CCSpriteFrameCache* spriteFrameCache = CCSpriteFrameCache::sharedSpriteFrameCache();  
  3.   
  4.     CCSpriteFrame* frame = NULL;  
  5.   
  6.     CCArray* frameArray = CCArray::create();  
  7.     for(int i = 1; i <= iFrameNum; i++)  
  8.     {  
  9.         frame = spriteFrameCache->spriteFrameByName(  
  10.             CCString::createWithFormat("%s%d.png", singleName, i)->getCString());  
  11.   
  12.         frameArray->addObject(frame);  
  13.   
  14.     }   
  15.   
  16.     return CCAnimation::createWithSpriteFrames(frameArray);  
  17. }  
CCAnimation* AnimationUtil::createAnimationWithSingleName( const char* singleName, int iFrameNum ) {
	CCSpriteFrameCache* spriteFrameCache = CCSpriteFrameCache::sharedSpriteFrameCache();

	CCSpriteFrame* frame = NULL;

	CCArray* frameArray = CCArray::create();
	for(int i = 1; i <= iFrameNum; i++)
	{
		frame = spriteFrameCache->spriteFrameByName(
			CCString::createWithFormat("%s%d.png", singleName, i)->getCString());

		frameArray->addObject(frame);

	} 

	return CCAnimation::createWithSpriteFrames(frameArray);
}

当然了,我默认动作图片都是用TexturePacker打包过的,这不在本文的讨论范围内。

(小若:我不懂,难道我还会告诉你?)

这是我们用到的图片资源:

这是图片对应的配置文件,大家可以双击打开看看:

(小若:啊!看你妹纸啊!怎么双击啊,你击给我看啊!明明是图片啊好不好!)

由于hero1.plist文件太长了,我就不贴出来了,大家自己随便弄一个动作资源吧。

(小若:你今天的心情是有多不好。。。)

然后试试使用这个函数:

  1. CCAnimation* heroAnim = AnimationUtil::sharedAnimationUtil()->createAnimationWithSingleName("hero1_atk", 6);  
  2.     heroAnim->setLoops(-1);  
  3.     heroAnim->setDelayPerUnit(0.1f);  
  4.   
  5.     sprite->runAction(CCAnimate::create(heroAnim));  
CCAnimation* heroAnim = AnimationUtil::sharedAnimationUtil()->createAnimationWithSingleName("hero1_atk", 6);
	heroAnim->setLoops(-1);
	heroAnim->setDelayPerUnit(0.1f);

	sprite->runAction(CCAnimate::create(heroAnim));


 

挺简单的,挺好的,指定了图片资源的名称,指定了图片资源的帧数,然后createAnimationWithSingleName就会创建好CCAnimation对象。这个方法在百度一搜,也能搜到不少。

这是一个很不错的方法,只需用一句话就能创建一个动画对象,很好,那么,本次教程就到此结束了。

(小若:总感觉有什么不对劲。。。。不对啊!你不是要告诉我们更好的方法吗?!)

不过,有个更方便的方法:

  1. CCAnimation* AnimationUtil::createAnimationWithSingleName( const char* singleName ) {  
  2.     CCSpriteFrameCache* spriteFrameCache = CCSpriteFrameCache::sharedSpriteFrameCache();  
  3.   
  4.     int index = 1;  
  5.     CCSpriteFrame* frame = NULL;  
  6.   
  7.     CCArray* frameArray = CCArray::create();  
  8.     do   
  9.     {  
  10.         frame = spriteFrameCache->spriteFrameByName(  
  11.             CCString::createWithFormat("%s%d.png", singleName, index)->getCString());  
  12.   
  13.         if(frame == NULL) {  
  14.             break;  
  15.         }  
  16.   
  17.         frameArray->addObject(frame);  
  18.         index++;  
  19.   
  20.     } while (true);  
  21.   
  22.     return CCAnimation::createWithSpriteFrames(frameArray);  
  23. }  
CCAnimation* AnimationUtil::createAnimationWithSingleName( const char* singleName ) {
	CCSpriteFrameCache* spriteFrameCache = CCSpriteFrameCache::sharedSpriteFrameCache();

	int index = 1;
	CCSpriteFrame* frame = NULL;

	CCArray* frameArray = CCArray::create();
	do 
	{
		frame = spriteFrameCache->spriteFrameByName(
			CCString::createWithFormat("%s%d.png", singleName, index)->getCString());

		if(frame == NULL) {
			break;
		}

		frameArray->addObject(frame);
		index++;

	} while (true);

	return CCAnimation::createWithSpriteFrames(frameArray);
}


 

和之前的方法唯一的区别就是:

1. For循环换成了do while循环

2. 不需要指定帧数

(小若:唯一的区别竟然有两点。。。唯一。。。唯一是这样的吗!唯一那就应该只有一点啊!)

这个函数会根据指定的资源名称一直加载图片帧,直到遇到空值为止,虽然这有点投机取巧的味道,但是可以省去一件很麻烦的事情,那就是:指定动画的帧数。

我们把图片用TexturePacker打包好之后,不可能每次加载一个动画,就打开pilist文件去看看这个动画有多少帧吧?反正我是觉得这样好麻烦,所以就想出这个投机的方法。

仅供参考~

原文地址:https://www.cnblogs.com/wodehao0808/p/3465809.html