日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

跟着石头哥哥学cocos2d-x(四)--cocos2dx中的动画以及TexturePacker使用

發(fā)布時(shí)間:2023/12/19 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 跟着石头哥哥学cocos2d-x(四)--cocos2dx中的动画以及TexturePacker使用 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>


之前向Andreas?Loew申請(qǐng)了一枚TexturePacker注冊(cè)碼,很快都下來(lái)了,作為回報(bào)我打算還是寫(xiě)一篇關(guān)于TexturePacker的使用博客吧,有興趣的可以在這里申請(qǐng)密鑰,http://www.codeandweb.com,前不久他也來(lái)信回訪過(guò)問(wèn)我使用感覺(jué)怎樣,因?yàn)楣ぷ鞯脑?#xff0c;太忙了,這里我主要介紹下cocos2dx中的動(dòng)畫(huà)以及TexturePacker使用。

做動(dòng)畫(huà)之前,要準(zhǔn)備素材,在demo中的你可以下載:http://pan.baidu.com/share/link?shareid=1419818902&uk=3088193979

打開(kāi)TexturePacker,

這個(gè)相信大家都看得懂,這里選擇cocos2d,然后添加文件夾,該工具會(huì)自動(dòng)加載紋理圖片,選擇打包的紋理圖片以及plist輸出路徑:


各項(xiàng)參數(shù)設(shè)定完畢,發(fā)布,你會(huì)在設(shè)置的輸出路徑得到打包好的圖片以及一個(gè)plist文件;

接下來(lái),介紹下動(dòng)畫(huà)的創(chuàng)作:

動(dòng)畫(huà)創(chuàng)作有很多中方式,這里我就采用plist方式來(lái)創(chuàng)建動(dòng)畫(huà);注意幾點(diǎn) 1.精靈幀緩沖;2.精靈幀;3.動(dòng)畫(huà)序列容器;ps:再次建議學(xué)2dx的話,有精力, 把源碼翻來(lái)看看,因?yàn)槲覀兒芏嗍怯洸蛔〉?但是原理一定要明白。

這里直接上創(chuàng)建動(dòng)畫(huà)步驟,demo代碼片段:

1、加載plist到緩沖幀里面,這里通過(guò)數(shù)組來(lái)創(chuàng)建動(dòng)畫(huà)幀序列;

2.將精靈幀添加到數(shù)組中;

3.創(chuàng)建精靈;

4.創(chuàng)建動(dòng)畫(huà)序列幀(注意動(dòng)畫(huà)序列容易這里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ù)組來(lái)保存幀動(dòng)畫(huà)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);//通過(guò)數(shù)組中的第一個(gè)精靈幀 創(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)建動(dòng)畫(huà)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動(dòng)作序列容器 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í)行動(dòng)作 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; }



運(yùn)行效果如下:


源碼在這里以及打包好的資源:http://pan.baidu.com/share/link?shareid=1535353761&uk=3088193979

ps:關(guān)于TexturePacker作者,很厲害 也是很熱情的 ,key在你申請(qǐng)之后會(huì)很快發(fā)放下來(lái)呵呵,感謝他提供這樣優(yōu)秀的工具!











轉(zhuǎn)載于:https://my.oschina.net/chenleijava/blog/185420

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的跟着石头哥哥学cocos2d-x(四)--cocos2dx中的动画以及TexturePacker使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。