Cocos2d_x 精靈動畫CCAnimation

CCAnimation

其實(shí)這個類就是封裝了一個Frame序列迁酸,作為精靈播放動畫的參數(shù),沒有別的功能条获。

Animation* Animation::createWithSpriteFrames(const Vector<SpriteFrame*>& frames, float delay, unsigned int loops)
根據(jù)一個Frame數(shù)組構(gòu)建CCAnimation對象疆栏,在CCAnimation中Frame序列就是用數(shù)組保存的
delay:設(shè)置了換幀的時間間隔贬媒,默認(rèn)為0

-(void) addFrame:(CCSpriteFrame*)frame
向Frame序列中添加一個CCSpriteFrame對象洋魂。

-(void) addFrameWithFilename:(NSString*)filename
根據(jù)資源的文件名創(chuàng)建一個Frame并添加到序列中绷旗。

-(void) addFrameWithTexture:(CCTexture2D*)texture rect:(CGRect)rect
根據(jù)一個Texture創(chuàng)建一個Frame并添加到序列中。

-(void) setRestoreOriginalFrame(bool restoreOriginalFrame)
設(shè)置是否在動畫播放結(jié)束后恢復(fù)到第一幀

CCAnimationCache

是用來緩存CCAnimation的副砍,是一個單例
Cocos2d-x中衔肢,動畫的具體內(nèi)容是依靠精靈顯示出來的,為了顯示動態(tài)圖片豁翎,我們需要不停切換精靈顯示的內(nèi)容角骤,通過把靜態(tài)的精靈變?yōu)閯赢嫴シ牌鲝亩鴮?shí)現(xiàn)動畫效果。動畫由幀組成心剥,每一幀都是一個紋理邦尊,我們可以使用一個紋理序列來創(chuàng)建動畫。

我們使用Animation類描述一個動畫刘陶,而精靈顯示動畫的動作則是一個Animate對象胳赌。動畫動作Animate是精靈顯示動畫的動作牢撼,它由一個動畫對象創(chuàng)建匙隔,并由精靈執(zhí)行。

+ (CCAnimationCache *)sharedAnimationCache
獲取單例

void addAnimation(Animation *animation, const std::string& name);
向池中添加一個CCAnimation對象熏版。

void removeAnimation(const std::string& name);
根據(jù)key從池中刪除CCAnimation對象纷责。

Animation *animationByName(const std::string& name) { return getAnimation(name);}
根據(jù)key從池中獲取CCAnimation對象。

-(void)addAnimationsWithDictionary:(NSDictionary *)dictionary
根據(jù)一個字典創(chuàng)建CCAnimation對象撼短,并存入內(nèi)存池再膳。字典中保存的是每個動畫的名字,動畫中包含哪些幀曲横,換幀間隔是多長喂柒。

-(void)addAnimationsWithFile:(NSString *)plist
根據(jù)一個plist文件創(chuàng)建CCAnimation對象,并存入內(nèi)存池禾嫉。先用文件中的信息生成一個字典灾杰,再調(diào)用addAnimationsWithDictionary方法來實(shí)現(xiàn)。

CCAnimate

這是一個持續(xù)型行為類熙参,父類是CCActionInterval艳吠,它的作用就是根據(jù)CCAnimation中的序列及間隔時間,不斷地切換精靈的幀孽椰,使其產(chǎn)生動畫效果昭娩。

+ (id) actionWithAnimation: (CCAnimation*)anim
根據(jù)CCSpriteFrame對象生成一個動畫播放行為凛篙,持續(xù)的時間由幀數(shù)和間隔時間相乘算出。

+ (id) actionWithAnimation: (CCAnimation*)anim restoreOriginalFrame:(BOOL)b
b為yes時栏渺,當(dāng)動畫播放完畢后會切換回播放前顯示的幀呛梆。

+ (id) actionWithDuration:(ccTime)duration animation: (CCAnimation*)anim restoreOriginalFrame:(BOOL)b
手動設(shè)置動畫的播放時間,時間到動畫才算結(jié)束磕诊。

cocos2d_x 播放幀動畫主要流程

(1)創(chuàng)建CCSpriteFrame數(shù)組削彬,可以使用CCSpriteFrameCache或者CCTextureCache
(2)通過幀序列創(chuàng)建CCAnimation對象
(3)通過CCAnimation對象和間隔時間創(chuàng)建CCAnimate秀仲,生成一個持續(xù)性動作融痛。
(4)使用精靈執(zhí)行動作

動畫創(chuàng)建方法(LUA)

(1)手動添加序列幀到Animation類

local png_file = "sprite.png"
local sprite = self:create_sprite(png_file)
sprite:setPosition(cc.p(0,0))
sprite:setScale(13)
self.view:addChild(sprite)

local texture =  cc.Director:getInstance():getTextureCache():addImage(png_file)
local animation = cc.Animation:create()

for i = 1, 10 do --從紋理中扣了10幀frame,組成序列幀數(shù)組
    local rect = cc.rect(i * 18, 0, 18, 32)
    local spriteFrame = cc.SpriteFrame:createWithTexture(texture,rect)
    animation:addSpriteFrame(spriteFrame)
end

animation:setDelayPerUnit(0.1); --播放兩張圖片的間隔時間
animation:setRestoreOriginalFrame(true); --動畫執(zhí)行后還原初始狀態(tài)
local action = cc.Animate:create(animation);
local repeat_action = cc.RepeatForever:create(action)
sprite:runAction(repeat_action);

function GameController:create_sprite(png)
    if png == nil then
        Log("png is nil!!")
        return nil
    end

    local sprite_frame = cc.SpriteFrameCache:getInstance():getSpriteFrame(png)
    local sprite = nil
    if sprite_frame then
        sprite = cc.Sprite:createWithSpriteFrame(sprite_frame)
    else
        local texture = cc.Director:getInstance():getTextureCache():addImage(png)
        if texture then
            sprite = cc.Sprite:createWithTexture(texture)
        else
            Log("紋理不存在", png)
        end
    end

    return sprite
end

效果:


sprite.gif

附:有各種不同動作的圖片:sprite.png


sprite.jpg

(2)使用文件初始化Animation類
AnimationCache可以加載xml/plist文件,plist文件里保存了組成動畫的相關(guān)信息神僵,通過該類獲取到plist文件里的動畫雁刷。

使用文件添加的方法只需將創(chuàng)建好的plist文件添加到動畫緩存里面,plist文件里包含了序列幀的相關(guān)信息保礼。再用動畫緩存初始化Animation實(shí)例沛励,用Animate實(shí)例來播放序列幀動畫。

local png_file = "PFBoom.png"
local sprite = self:create_sprite(png_file)
sprite:setPosition(cc.p(0,0))
sprite:setScale(10)
self.view:addChild(sprite)

local animation = cc.Animation:create()
animation:setDelayPerUnit(0.1); --播放兩張圖片的間隔時間

local cache = cc.SpriteFrameCache:getInstance()
cache:addSpriteFrames("PFBoom.plist") --添加plist文件

for i = 1, 18 do
    local png_name = "Boom_"..i..".png"
    local spriteFrame = cache:getSpriteFrame(png_name)
    animation:addSpriteFrame(spriteFrame)
end

local action = cc.Animate:create(animation);
local call_back = cc.CallFunc:create(handler(self, function ()
    Log("call_back")
end))
local seq = cc.Sequence:create(action, call_back)
local repeat_action = cc.RepeatForever:create(seq)
sprite:runAction(repeat_action)

效果:


PFBoom.gif

附:有各種不同動作的圖片:PFBoom.png


PFBoom.png

plist文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>frames</key>
    <dict>
        <key>Boom_1.png</key>
        <dict>
            <key>frame</key>
            <string>{{204,305},{99,99}}</string>
            <key>offset</key>
            <string>{0,0}</string>
            <key>rotated</key>
            <false/>
            <key>sourceColorRect</key>
            <string>{{0,0},{99,99}}</string>
            <key>sourceSize</key>
            <string>{99,99}</string>
        </dict>
        <key>Boom_10.png</key>
        <dict>
            <key>frame</key>
            <string>{{103,307},{99,99}}</string>
            <key>offset</key>
            <string>{0,0}</string>
            <key>rotated</key>
            <false/>
            <key>sourceColorRect</key>
            <string>{{0,0},{99,99}}</string>
            <key>sourceSize</key>
            <string>{99,99}</string>
        </dict>
        <key>Boom_11.png</key>
        <dict>
            <key>frame</key>
            <string>{{103,206},{99,99}}</string>
            <key>offset</key>
            <string>{0,0}</string>
            <key>rotated</key>
            <false/>
            <key>sourceColorRect</key>
            <string>{{0,0},{99,99}}</string>
            <key>sourceSize</key>
            <string>{99,99}</string>
        </dict>
        <key>Boom_12.png</key>
        <dict>
            <key>frame</key>
            <string>{{105,2},{99,99}}</string>
            <key>offset</key>
            <string>{0,0}</string>
            <key>rotated</key>
            <false/>
            <key>sourceColorRect</key>
            <string>{{0,0},{99,99}}</string>
            <key>sourceSize</key>
            <string>{99,99}</string>
        </dict>
        <key>Boom_13.png</key>
        <dict>
            <key>frame</key>
            <string>{{103,105},{99,99}}</string>
            <key>offset</key>
            <string>{0,0}</string>
            <key>rotated</key>
            <false/>
            <key>sourceColorRect</key>
            <string>{{0,0},{99,99}}</string>
            <key>sourceSize</key>
            <string>{99,99}</string>
        </dict>
        <key>Boom_14.png</key>
        <dict>
            <key>frame</key>
            <string>{{2,2},{101,101}}</string>
            <key>offset</key>
            <string>{0,0}</string>
            <key>rotated</key>
            <false/>
            <key>sourceColorRect</key>
            <string>{{0,0},{101,101}}</string>
            <key>sourceSize</key>
            <string>{101,101}</string>
        </dict>
        <key>Boom_15.png</key>
        <dict>
            <key>frame</key>
            <string>{{2,408},{99,99}}</string>
            <key>offset</key>
            <string>{0,0}</string>
            <key>rotated</key>
            <false/>
            <key>sourceColorRect</key>
            <string>{{0,0},{99,99}}</string>
            <key>sourceSize</key>
            <string>{99,99}</string>
        </dict>
        <key>Boom_16.png</key>
        <dict>
            <key>frame</key>
            <string>{{2,307},{99,99}}</string>
            <key>offset</key>
            <string>{0,0}</string>
            <key>rotated</key>
            <false/>
            <key>sourceColorRect</key>
            <string>{{0,0},{99,99}}</string>
            <key>sourceSize</key>
            <string>{99,99}</string>
        </dict>
        <key>Boom_17.png</key>
        <dict>
            <key>frame</key>
            <string>{{2,206},{99,99}}</string>
            <key>offset</key>
            <string>{0,0}</string>
            <key>rotated</key>
            <false/>
            <key>sourceColorRect</key>
            <string>{{0,0},{99,99}}</string>
            <key>sourceSize</key>
            <string>{99,99}</string>
        </dict>
        <key>Boom_18.png</key>
        <dict>
            <key>frame</key>
            <string>{{2,105},{99,99}}</string>
            <key>offset</key>
            <string>{0,0}</string>
            <key>rotated</key>
            <false/>
            <key>sourceColorRect</key>
            <string>{{0,0},{99,99}}</string>
            <key>sourceSize</key>
            <string>{99,99}</string>
        </dict>
        <key>Boom_2.png</key>
        <dict>
            <key>frame</key>
            <string>{{204,204},{99,99}}</string>
            <key>offset</key>
            <string>{0,0}</string>
            <key>rotated</key>
            <false/>
            <key>sourceColorRect</key>
            <string>{{0,0},{99,99}}</string>
            <key>sourceSize</key>
            <string>{99,99}</string>
        </dict>
        <key>Boom_3.png</key>
        <dict>
            <key>frame</key>
            <string>{{406,103},{99,99}}</string>
            <key>offset</key>
            <string>{0,0}</string>
            <key>rotated</key>
            <false/>
            <key>sourceColorRect</key>
            <string>{{0,0},{99,99}}</string>
            <key>sourceSize</key>
            <string>{99,99}</string>
        </dict>
        <key>Boom_4.png</key>
        <dict>
            <key>frame</key>
            <string>{{305,103},{99,99}}</string>
            <key>offset</key>
            <string>{0,0}</string>
            <key>rotated</key>
            <false/>
            <key>sourceColorRect</key>
            <string>{{0,0},{99,99}}</string>
            <key>sourceSize</key>
            <string>{99,99}</string>
        </dict>
        <key>Boom_5.png</key>
        <dict>
            <key>frame</key>
            <string>{{204,103},{99,99}}</string>
            <key>offset</key>
            <string>{0,0}</string>
            <key>rotated</key>
            <false/>
            <key>sourceColorRect</key>
            <string>{{0,0},{99,99}}</string>
            <key>sourceSize</key>
            <string>{99,99}</string>
        </dict>
        <key>Boom_6.png</key>
        <dict>
            <key>frame</key>
            <string>{{408,2},{99,99}}</string>
            <key>offset</key>
            <string>{0,0}</string>
            <key>rotated</key>
            <false/>
            <key>sourceColorRect</key>
            <string>{{0,0},{99,99}}</string>
            <key>sourceSize</key>
            <string>{99,99}</string>
        </dict>
        <key>Boom_7.png</key>
        <dict>
            <key>frame</key>
            <string>{{307,2},{99,99}}</string>
            <key>offset</key>
            <string>{0,0}</string>
            <key>rotated</key>
            <false/>
            <key>sourceColorRect</key>
            <string>{{0,0},{99,99}}</string>
            <key>sourceSize</key>
            <string>{99,99}</string>
        </dict>
        <key>Boom_8.png</key>
        <dict>
            <key>frame</key>
            <string>{{206,2},{99,99}}</string>
            <key>offset</key>
            <string>{0,0}</string>
            <key>rotated</key>
            <false/>
            <key>sourceColorRect</key>
            <string>{{0,0},{99,99}}</string>
            <key>sourceSize</key>
            <string>{99,99}</string>
        </dict>
        <key>Boom_9.png</key>
        <dict>
            <key>frame</key>
            <string>{{103,408},{99,99}}</string>
            <key>offset</key>
            <string>{0,0}</string>
            <key>rotated</key>
            <false/>
            <key>sourceColorRect</key>
            <string>{{0,0},{99,99}}</string>
            <key>sourceSize</key>
            <string>{99,99}</string>
        </dict>
    </dict>
    <key>metadata</key>
    <dict>
        <key>format</key>
        <integer>2</integer>
        <key>realTextureFileName</key>
        <string>PFBoom.png</string>
        <key>size</key>
        <string>{512,512}</string>
        <key>textureFileName</key>
        <string>PFBoom.png</string>
    </dict>
</dict>
</plist>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末炮障,一起剝皮案震驚了整個濱河市目派,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌胁赢,老刑警劉巖企蹭,帶你破解...
    沈念sama閱讀 217,826評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異智末,居然都是意外死亡谅摄,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評論 3 395
  • 文/潘曉璐 我一進(jìn)店門系馆,熙熙樓的掌柜王于貴愁眉苦臉地迎上來送漠,“玉大人,你說我怎么就攤上這事由蘑∶龉眩” “怎么了?”我有些...
    開封第一講書人閱讀 164,234評論 0 354
  • 文/不壞的土叔 我叫張陵尼酿,是天一觀的道長爷狈。 經(jīng)常有香客問我,道長谓媒,這世上最難降的妖魔是什么淆院? 我笑而不...
    開封第一講書人閱讀 58,562評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上土辩,老公的妹妹穿的比我還像新娘支救。我一直安慰自己,他們只是感情好拷淘,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,611評論 6 392
  • 文/花漫 我一把揭開白布各墨。 她就那樣靜靜地躺著,像睡著了一般启涯。 火紅的嫁衣襯著肌膚如雪贬堵。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,482評論 1 302
  • 那天结洼,我揣著相機(jī)與錄音黎做,去河邊找鬼。 笑死松忍,一個胖子當(dāng)著我的面吹牛蒸殿,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播鸣峭,決...
    沈念sama閱讀 40,271評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼宏所,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了摊溶?” 一聲冷哼從身側(cè)響起爬骤,我...
    開封第一講書人閱讀 39,166評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎莫换,沒想到半個月后霞玄,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,608評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡浓镜,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,814評論 3 336
  • 正文 我和宋清朗相戀三年溃列,在試婚紗的時候發(fā)現(xiàn)自己被綠了劲厌。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片膛薛。...
    茶點(diǎn)故事閱讀 39,926評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖补鼻,靈堂內(nèi)的尸體忽然破棺而出哄啄,到底是詐尸還是另有隱情,我是刑警寧澤风范,帶...
    沈念sama閱讀 35,644評論 5 346
  • 正文 年R本政府宣布咨跌,位于F島的核電站,受9級特大地震影響硼婿,放射性物質(zhì)發(fā)生泄漏锌半。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,249評論 3 329
  • 文/蒙蒙 一寇漫、第九天 我趴在偏房一處隱蔽的房頂上張望刊殉。 院中可真熱鬧殉摔,春花似錦、人聲如沸记焊。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽遍膜。三九已至碗硬,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間瓢颅,已是汗流浹背恩尾。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留挽懦,地道東北人特笋。 一個月前我還...
    沈念sama閱讀 48,063評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像巾兆,于是被迫代替她去往敵國和親猎物。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,871評論 2 354

推薦閱讀更多精彩內(nèi)容