2019獨角獸企業(yè)重金招聘Python工程師標準>>>
之前向Andreas?Loew申請了一枚TexturePacker注冊碼,很快都下來了,作為回報我打算還是寫一篇關(guān)于TexturePacker的使用博客吧,有興趣的可以在這里申請密鑰,http://www.codeandweb.com,前不久他也來信回訪過問我使用感覺怎樣,因為工作的原因,太忙了,這里我主要介紹下cocos2dx中的動畫以及TexturePacker使用。
做動畫之前,要準備素材,在demo中的你可以下載:http://pan.baidu.com/share/link?shareid=1419818902&uk=3088193979
打開TexturePacker,
這個相信大家都看得懂,這里選擇cocos2d,然后添加文件夾,該工具會自動加載紋理圖片,選擇打包的紋理圖片以及plist輸出路徑:
各項參數(shù)設(shè)定完畢,發(fā)布,你會在設(shè)置的輸出路徑得到打包好的圖片以及一個plist文件;
接下來,介紹下動畫的創(chuàng)作:
動畫創(chuàng)作有很多中方式,這里我就采用plist方式來創(chuàng)建動畫;注意幾點 1.精靈幀緩沖;2.精靈幀;3.動畫序列容器;ps:再次建議學(xué)2dx的話,有精力, 把源碼翻來看看,因為我們很多是記不住的 但是原理一定要明白。
這里直接上創(chuàng)建動畫步驟,demo代碼片段:
1、加載plist到緩沖幀里面,這里通過數(shù)組來創(chuàng)建動畫幀序列;
2.將精靈幀添加到數(shù)組中;
3.創(chuàng)建精靈;
4.創(chuàng)建動畫序列幀(注意動畫序列容易這里CCSequence用類型),最后回調(diào)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");//創(chuàng)建幀數(shù)組--數(shù)組來保存幀動畫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){//從緩存中獲取精靈幀添加到數(shù)組中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){//從緩存中獲取精靈幀添加到數(shù)組中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()));}//創(chuàng)建攻擊類型1 精靈CCSprite * sp=CCSprite::createWithSpriteFrame((CCSpriteFrame*)attackArray->objectAtIndex(0));sp->setPosition(ccp(mysize.width/4,mysize.height/3));this->addChild(sp);//創(chuàng)建攻擊類型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);//通過數(shù)組中的第一個精靈幀 創(chuàng)建奔跑精靈 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);//創(chuàng)建動畫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);//執(zhí)行動作 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:關(guān)于TexturePacker作者,很厲害 也是很熱情的 ,key在你申請之后會很快發(fā)放下來呵呵,感謝他提供這樣優(yōu)秀的工具!
轉(zhuǎn)載于:https://my.oschina.net/chenleijava/blog/185420
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎
總結(jié)
以上是生活随笔為你收集整理的跟着石头哥哥学cocos2d-x(四)--cocos2dx中的动画以及TexturePacker使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。