您的位置:首页 > 其它

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

2013-12-09 17:39 316 查看
原文链接:/article/1389420.html

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

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

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

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

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

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

转载请注明,原文地址:/article/1389420.html

正文:

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

[cpp] view plaincopyprint?

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);

}

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文件太长了,我就不贴出来了,大家自己随便弄一个动作资源吧。

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

然后试试使用这个函数:

[cpp] view plaincopyprint?

CCAnimation* heroAnim = AnimationUtil::sharedAnimationUtil()->createAnimationWithSingleName("hero1_atk", 6);

heroAnim->setLoops(-1);

heroAnim->setDelayPerUnit(0.1f);

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对象。这个方法在百度一搜,也能搜到不少。

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

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

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

[cpp] view plaincopyprint?

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);

}

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文件去看看这个动画有多少帧吧?反正我是觉得这样好麻烦,所以就想出这个投机的方法。

仅供参考~!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: