2019独角兽企业重金招聘Python工程师标准>>>
之前向Andreas Loew申请了一枚TexturePacker注册码,很快都下来了,作为回报我打算还是写一篇关于TexturePacker的使用博客吧,有兴趣的可以在这里申请密钥,http://www.codeandweb.com,前不久他也来信回访过问我使用感觉怎样,因为工作的原因,太忙了,这里我主要介绍下cocos2dx中的动画以及TexturePacker使用。
做动画之前,要准备素材,在demo中的你可以下载:http://pan.baidu.com/share/link?shareid=1419818902&uk=3088193979
打开TexturePacker,
这个相信大家都看得懂,这里选择cocos2d,然后添加文件夹,该工具会自动加载纹理图片,选择打包的纹理图片以及plist输出路径:
各项参数设定完毕,发布,你会在设置的输出路径得到打包好的图片以及一个plist文件;
接下来,介绍下动画的创作:
动画创作有很多中方式,这里我就采用plist方式来创建动画;注意几点 1.精灵帧缓冲;2.精灵帧;3.动画序列容器;ps:再次建议学2dx的话,有精力, 把源码翻来看看,因为我们很多是记不住的 但是原理一定要明白。
这里直接上创建动画步骤,demo代码片段:
1、加载plist到缓冲帧里面,这里通过数组来创建动画帧序列;
2.将精灵帧添加到数组中;
3.创建精灵;
4.创建动画序列帧(注意动画序列容易这里CCSequence用类型),最后回调runAction(CCAction *action);
#ifndef __ROLE__HH__
#define __ROLE__HH__
#include "cocos2d.h"
USING_NS_CC;
class role:public CCLayer
{
public:role(void);~role(void);virtual bool init(); static cocos2d::CCScene* scene();CREATE_FUNC(role);
};
#endif
#include "role.h"role::role(void)
{
}role::~role(void)
{
}bool role::init()
{bool bRet = false;do {//-new-//CCSize mysize=CCDirector::sharedDirector()->getWinSize();//把role.plist加入缓存帧CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("role/role.plist");//创建帧数组--数组来保存帧动画CCArray * attackArray=CCArray::create();//attackArray->retain();CCArray * attackArray2=CCArray::create();//attackArray2->retain();CCArray * runArray=CCArray::create();//runArray->retain();CCArray * walkArray=CCArray::create();for (int index=1;index!=9;++index){//从缓存中获取精灵帧添加到数组中CCLOG(CCString::createWithFormat("%s%d.png","Img_Zhici",index)->getCString());attackArray->addObject(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(CCString::createWithFormat("%s%d.png","Img_Zhici",index)->getCString()));}//Img_Zhn1.pngfor (int i=1;i!=17;++i){//从缓存中获取精灵帧添加到数组中CCLOG(CCString::createWithFormat("%s%d.png","Img_Zhn",i)->getCString());attackArray2->addObject(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(CCString::createWithFormat("%s%d.png","Img_Zhn",i)->getCString()));}//run for (int i=1;i!=7;++i){CCLOG(CCString::createWithFormat("%s%d.png","Img_ZRun",i)->getCString());runArray->addObject(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(CCString::createWithFormat("%s%d.png","Img_ZRun",i)->getCString()));}//walkfor (int i=1;i!=7;++i){CCString::createWithFormat("%s%d.png","Img_Zwlak",i)->getCString();walkArray->addObject(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(CCString::createWithFormat("%s%d.png","Img_Zwlak",i)->getCString()));}//创建攻击类型1 精灵CCSprite * sp=CCSprite::createWithSpriteFrame((CCSpriteFrame*)attackArray->objectAtIndex(0));sp->setPosition(ccp(mysize.width/4,mysize.height/3));this->addChild(sp);//创建攻击类型2精灵CCSprite * standAttack=CCSprite::createWithSpriteFrame((CCSpriteFrame*)attackArray2->objectAtIndex(0)); CCSize visibleSize= CCDirector::sharedDirector()->getVisibleSize();standAttack->setPosition(ccp(visibleSize.width/3,visibleSize.height/3));this->addChild(standAttack);//通过数组中的第一个精灵帧 创建奔跑精灵 CCSprite *runsprite=CCSprite::createWithSpriteFrame((CCSpriteFrame*)runArray->objectAtIndex(0));runsprite->setPosition(ccp(visibleSize.width/2,visibleSize.height/3));this->addChild(runsprite);CCSprite *walkSprite=CCSprite::createWithSpriteFrame((CCSpriteFrame*)walkArray->objectAtIndex(0));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动作序列容器 CCSpawnCCSequence* pse1=CCSequence::create(attack1,NULL);CCSequence* pse2=CCSequence::create(standAnimate,NULL);CCSequence* pse3=CCSequence::create(runAnimate,NULL);CCSequence* pse4=CCSequence::create(walkAnimate,NULL);//执行动作 forerversp->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;
}cocos2d::CCScene* role::scene()
{CCScene * scene = NULL;do {scene = CCScene::create();CC_BREAK_IF(! scene);role *layer = role::create();CC_BREAK_IF(! layer);scene->addChild(layer);} while (0);return scene;
}
源码在这里以及打包好的资源:http://pan.baidu.com/share/link?shareid=1535353761&uk=3088193979
ps:关于TexturePacker作者,很厉害 也是很热情的 ,key在你申请之后会很快发放下来呵呵,感谢他提供这样优秀的工具!