cocos2dx中的动画以及TexturePacker使用

cocos2dx中的动画以及TexturePacker使用,第1张

概述最近看到一篇文章,讲的是TexturePacker将图片打包,在cocos2dx中使用的一个教程,因为原作者使用的是cocos2dx-2.x的版本,所以在使用作者源代码写demo的时候,会有一些报错,因为环境更改的原因,只要修改几个小地方就可以实现了,在这里做个笔记,方便以后浏览和学习。 首先贴出原作者的帖子:http://my.oschina.net/chenleijava/blog/18542

最近看到一篇文章,讲的是TexturePacker将图片打包,在cocos2dx中使用的一个教程,因为原作者使用的是cocos2dx-2.x的版本,所以在使用作者源代码写demo的时候,会有一些报错,因为环境更改的原因,只要修改几个小地方就可以实现了,在这里做个笔记,方便以后浏览和学习。

首先贴出原作者的帖子:http://my.oschina.net/chenleijava/blog/185420#printSource


使用这部分代码会有部分的报错,原因大概就是新版本将Vector换掉了原来的CCArray。所以只要支持一下就好了,贴出我修改后的代码,基本与源代码保持不变,只贴出init()这个函数里面的变动:

bool role::init(){	bool bRet = false;	do	{		//-new-//		CCSize mysize = CCDirector::sharedDirector()->getWinSize();		//把role.pList加入缓存帧		CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithfile("role/role.pList");		//创建帧数组--数组来保存帧动画		Vector<SpriteFrame*> attackArray;		Vector<SpriteFrame*> attackArray2;		Vector<SpriteFrame*> runArray;		Vector<SpriteFrame*> walkArray;		for (int index = 1; index != 9; ++index)		{			//从缓存中获取精灵帧添加到数组中			cclOG(CCString::createWithFormat("%s%d.png","img_Zhici",index)->getCString());			SpriteFrame* s = CCSpriteFrameCache::sharedSpriteFrameCache()->				spriteFrameByname(CCString::createWithFormat("%s%d.png",index)->getCString());			attackArray.pushBack(s);		}		//img_Zhn1.png		for (int i = 1; i != 17; ++i)		{			//从缓存中获取精灵帧添加到数组中			cclOG(CCString::createWithFormat("%s%d.png","img_Zhn",i)->getCString());			attackArray2.pushBack(CCSpriteFrameCache::sharedSpriteFrameCache()->				spriteFrameByname(CCString::createWithFormat("%s%d.png",i)->getCString()));		}		//run 		for (int i = 1; i != 7; ++i)		{			cclOG(CCString::createWithFormat("%s%d.png","img_ZRun",i)->getCString());			runArray.pushBack(CCSpriteFrameCache::sharedSpriteFrameCache()->				spriteFrameByname(CCString::createWithFormat("%s%d.png",i)->getCString()));		}		//walk		for (int i = 1; i != 7; ++i){			CCString::createWithFormat("%s%d.png","img_Zwlak",i)->getCString();			walkArray.pushBack(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByname(				CCString::createWithFormat("%s%d.png",i)->getCString()));		}		//创建攻击类型1 精灵		CCSpriteFrame* a = attackArray.front();		CCSprite * sp = CCSprite::createWithSpriteFrame(a);		sp->setposition(ccp(mysize.wIDth / 4,mysize.height / 3));		this->addChild(sp);		//创建攻击类型2精灵		CCSprite * standAttack = CCSprite::createWithSpriteFrame((CCSpriteFrame*)attackArray2.front());		CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();		standAttack->setposition(ccp(visibleSize.wIDth / 3,visibleSize.height / 3));		this->addChild(standAttack);		//通过数组中的第一个精灵帧 创建奔跑精灵  		CCSprite *runsprite = CCSprite::createWithSpriteFrame((CCSpriteFrame*)runArray.front());		runsprite->setposition(ccp(visibleSize.wIDth / 2,visibleSize.height / 3));		this->addChild(runsprite);		CCSprite *walkSprite = CCSprite::createWithSpriteFrame((CCSpriteFrame*)walkArray.front());		walkSprite->setposition(ccp(visibleSize.wIDth / 1.5,visibleSize.height / 3));		this->addChild(walkSprite);		//创建动画		CCAnimation *animation1 = CCAnimation::createWithSpriteFrames(attackArray,0.1f);		CCAnimate *attack1 = CCAnimate::create(animation1);		CCAnimation * standAnimation = CCAnimation::createWithSpriteFrames(attackArray2,0.1f);		CCAnimate *standAnimate = CCAnimate::create(standAnimation);		CCAnimation * runAnimation = CCAnimation::createWithSpriteFrames(runArray,0.1f);		CCAnimate *runAnimate = CCAnimate::create(runAnimation);		CCAnimation * walkAnimation = CCAnimation::createWithSpriteFrames(walkArray,0.15f);		CCAnimate *walkAnimate = CCAnimate::create(walkAnimation);		//CCSequence动作序列容器 CCSpawn		CCSequence* pse1 = CCSequence::create(attack1,NulL);		CCSequence* pse2 = CCSequence::create(standAnimate,NulL);		CCSequence* pse3 = CCSequence::create(runAnimate,NulL);		CCSequence* pse4 = CCSequence::create(walkAnimate,NulL);		//执行动作 forerver		sp->runAction(CCRepeatForever::create(pse1));		standAttack->runAction(CCRepeatForever::create(pse2));		runsprite->runAction(CCRepeatForever::create(pse3));		walkSprite->runAction(CCRepeatForever::create(pse4));		CCSpriteFrameCache::sharedSpriteFrameCache()->removeSpriteFramesFromfile("role/role.pList");		bRet = true;	} while (0);	return bRet;}

有很多游戏公司也是用的这种方法来节省内存的使用,例如我从安卓版的《kingdom》游戏里面解出来的资源,格式非常的规范,一个png对应一个pList文件,随便找了个试试,结果不出所料,实现资源的加载。有兴趣的同学可以去试试。

总结

以上是内存溢出为你收集整理的cocos2dx中的动画以及TexturePacker使用全部内容,希望文章能够帮你解决cocos2dx中的动画以及TexturePacker使用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1070856.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存