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.png
(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.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>