cocos2dx創建精靈動作

玩遊戲的都知道,hero沒有動作,無論是走路,還是打架,都像站在滑板車上,像鬼魂一樣平移的飄來飄去,那是一種令人何其驚悚的景象呀!

所以在精靈設計的時候,我們需要給精靈添加一定的動作,比如走路時,腳動,打架時,手舞足蹈,打有打姿,跑有跑姿,這樣纔夠自然,因此我們需要用到cocostudio工具來生成plist文件,再使用這些資源配置來給精靈設置動作。(ps:cocostudio到官方網站下載對應系統的版本,至於使用,網上有很多資料)

話不多說了,開始這篇博客的旅程,


CCSprite *arrow = CCSprite::create("dammer0.png");//創建精靈初始狀態
CCSpriteFrameCache *arrowCache = CCSpriteFrameCache::sharedSpriteFrameCache();
arrowCache->addSpriteFramesWithFile("dammer.plist","dammer.png");//加載plist文件
CCSpriteFrame * arrowFrame = NULL;
CCArray *arrowArray = CCArray::create();
for(int i=1;i<3;i++)
{
	arrowFrame = arrowCache->spriteFrameByName(CCString::createWithFormat("dammer%d.png",i)->getCString());
	arrowArray->addObject(arrowFrame);//添加幀動畫
}
CCAnimation *arrowAnimation = CCAnimation::createWithSpriteFrames(arrowArray);//創建幀動畫
arrowAnimation->setLoops(1);//設置幀動畫動作循環次數,-1爲無限循環
arrowAnimation->setDelayPerUnit(0.08f);//設置每幀動畫的時延
CCAnimate *arrowAnimate = CCAnimate::create(arrowAnimation);//創建動畫動作
arrow->runAction(arrowAnimate);//精靈添加動作


最後結果如上圖所示!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章