cocos2d-x帧动画实现(写下备忘)

帧动画就是很多张png的序列图实现轮流播放产生动画效果。

那么首先我们要一套动画的序列图,没有图的可以看引擎例子里面的图。很多张图我们可以采用TP工具将它们压缩到一张png里面去,这样程序只需要读取一次就行了,提高效率。

动画是打地鼠地鼠出洞的表现,代码如下:

 

//新的游戏动画:
	//创建cache
	CCSpriteFrameCache* cache=CCSpriteFrameCache::sharedSpriteFrameCache();
	char strPlist[64]={0};
	char strPng[64]={0};
	sprintf(strPlist,"Game/Game_Resource.plist");
	sprintf(strPng,"Game/Game_Resource.png");
	cache->addSpriteFramesWithFile(strPlist,strPng);
	//创建动画每一帧,从cache中读取
	CCArray* animFrams=new CCArray(8);
	char str[64]={0};
	for (int i=0;i<3;i++)
	{
		sprintf(str,"mole_a%d.png",i);
		CCLog(str);
		CCSpriteFrame* frame=cache->spriteFrameByName(str);
		animFrams->addObject(frame);
	}
	CCAnimation* animation=CCAnimation::createWithSpriteFrames(animFrams,0.5f);
	animation->setDelayPerUnit(0.2f);
	CCAnimate* animate=CCAnimate::create(animation);
	shrew->runAction(CCRepeatForever::create(animate));


代码参考了这位作者 的文章,并针对2.0版引擎做了一点修改主要是文章中用的数组CCMutableArray改为了CCArray等。把代码写下来备忘。同时感谢原作者。

原文地址:https://www.cnblogs.com/zhong-dev/p/4044617.html