Cocos2dx 《基礎》 幀動畫

2021-08-19 17:21:18 字數 2395 閱讀 8610

《幀動畫》

a. spriteframe: 精靈幀。精靈幀包含了對應紋理在大的紋理區域中的位置和大小, 對應紋理是否經過旋轉和偏移。根據這些幾何資訊,,可以從大的紋理中找到正確的紋理區域作

為精靈幀顯示的影象。

使用紋理建立精靈幀

auto tex = texturecache::getinstance()->addimage("game/role.png");

auto spr = spriteframe::createwithtexture(tex,rect(0,0,81.25,81.25));

auto spi = sprite::createwithspriteframe(spr);

spi->setposition(vec2(visiblesize.width/2,visiblesize.height/2));

this->addchild(spi);

//使用plist檔案建立精靈幀

spriteframecache::getinstance()->addspriteframeswithfile("game/plist.plist");

auto sprite = sprite::createwithspriteframename("godplane.png");

sprite->setposition(vec2(visiblesize.width/2,visiblesize.height/2));

this->addchild(sprite);

b. spriteframecache: 精靈幀快取;存放了多個精靈幀到快取中,通過字典的方式儲存單個精靈幀,key: 精靈幀的名字,值: spriteframe。一般處理的是plist檔案。

plist檔案如果和對應的png在同乙個目錄下,則直接可以寫plist檔名就可以了。

c. animationframe: 動畫幀資訊,儲存的是對應的精靈幀資訊。通過animation的getframes()函式獲得。

e. animation: 動畫資訊。儲存了所有的動畫幀資訊, 一般通過create()或者createwithspriteframes()方法建立。

f.  animate: 動畫處理類。 真正完成動畫表演的類。

《建立幀動畫的流程:>

std::string animationname[4] = ;
void tieldmap::onenter()

void tieldmap::createanimation()

} //將動畫新增到動畫快取中

animationcache::getinstance()->addanimation(animation, animationname[i]); }}

void tieldmap::playanimation(int index)

; auto animation = animationcache::getinstance()->getanimation(animationname[index]);

auto sprite = sprite::create();

sprite->setposition(vec2(200, 300));

auto animate = animate::create(animation);

sprite->runaction(repeatforever::create(animate));

this->addchild(sprite);

}

《使用plist檔案建立動畫》

//使用plist檔案建立動畫

void tieldmap::createplist()

animation->addspriteframe(spriteframes);

} //將動畫放到動畫快取中

animationcache::getinstance()->addanimation(animation,"skill");

}void tieldmap::playanimationplist(std::string name)

; auto animation = animationcache::getinstance()->getanimation(name);

auto sprite = sprite::create();

sprite->setposition(vec2(500, 350));

auto animate = animate::create(animation);

sprite->runaction(repeatforever::create(animate));

this->addchild(sprite);

}

cocos2d x學習筆記 幀動畫

我們首先用tp將我們的資源壓縮成乙個大 包含plist和png檔案 然後就把這張大圖的這兩個檔案包含在我們的程式檔案裡,這樣我們就可以做有意思的動畫了。為了方便地記錄紋理的顯示資訊,cocos2d x提供了框幀類ccspriteframe。乙個框幀包含兩個屬性,紋理與區域。紋理指的是將要被顯示的紋理...

cocos2d x 連幀動畫實現

最開始的動畫片也是用疊加的方法,下面介紹如何有多張實現乙個小小的動畫 然後在你的init函式裡面 ccsprite p2 ccsprite spritewithfile name1.png 首先還是要建立乙個精靈物件 p2 setposition ccpointmake 240,160 this a...

cocos2d x幀動畫實現(續)

之前我介紹過cocos2d x的幀動畫實現,今天我把幀動畫詳細寫一下。那麼首先我們要一套動畫的序列圖,沒有圖的可以看引擎例子裡面的圖。很多張圖我們可以採用tp工具將它們壓縮到一張png裡面去,這樣程式只需要讀取一次就行了,提高效率。比如我將這裡的6張圖壓成了乙個png,tp會產生乙個所有圖的png和...