cocos2dx中的动画以及TexturePacker使用

前端之家收集整理的这篇文章主要介绍了cocos2dx中的动画以及TexturePacker使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

最近看到一篇文章,讲的是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文件,随便找了个试试,结果不出所料,实现资源的加载。有兴趣的同学可以去试试。

原文链接:https://www.f2er.com/cocos2dx/341421.html

猜你在找的Cocos2d-x相关文章