CCAnimation
其实这个类就是封装了一个Frame
序列,作为精灵播放动画的参数,没有别的功能。
Animation* Animation::createWithSpriteFrames(const Vector<SpriteFrame*>& frames, float delay, unsigned int loops)
根据一个Frame数组构建CCAnimation对象,在CCAnimation中Frame序列就是用数组保存的
delay:设置了换帧的时间间隔,默认为0
-(void) addFrame:(CCSpriteFrame*)frame
向Frame序列中添加一个CCSpriteFrame对象。
-(void) addFrameWithFilename:(NSString*)filename
根据资源的文件名创建一个Frame并添加到序列中。
-(void) addFrameWithTexture:(CCTexture2D*)texture rect:(CGRect)rect
根据一个Texture创建一个Frame并添加到序列中。
-(void) setRestoreOriginalFrame(bool restoreOriginalFrame)
设置是否在动画播放结束后恢复到第一帧
CCAnimationCache
是用来缓存CCAnimation
的,是一个单例
Cocos2d-x
中,动画的具体内容是依靠精灵显示出来的,为了显示动态图片,我们需要不停切换精灵显示的内容,通过把静态的精灵变为动画播放器从而实现动画效果。动画由帧组成,每一帧都是一个纹理,我们可以使用一个纹理序列来创建动画。
我们使用Animation
类描述一个动画,而精灵显示动画的动作则是一个Animate
对象。动画动作Animate
是精灵显示动画的动作,它由一个动画对象创建,并由精灵执行。
+ (CCAnimationCache *)sharedAnimationCache
获取单例
void addAnimation(Animation *animation, const std::string& name);
向池中添加一个CCAnimation对象。
void removeAnimation(const std::string& name);
根据key从池中删除CCAnimation对象。
Animation *animationByName(const std::string& name) { return getAnimation(name);}
根据key从池中获取CCAnimation对象。
-(void)addAnimationsWithDictionary:(NSDictionary *)dictionary
根据一个字典创建CCAnimation对象,并存入内存池。字典中保存的是每个动画的名字,动画中包含哪些帧,换帧间隔是多长。
-(void)addAnimationsWithFile:(NSString *)plist
根据一个plist文件创建CCAnimation对象,并存入内存池。先用文件中的信息生成一个字典,再调用addAnimationsWithDictionary方法来实现。
CCAnimate
这是一个持续型行为类,父类是CCActionInterval
,它的作用就是根据CCAnimation
中的序列及间隔时间,不断地切换精灵的帧,使其产生动画效果。
+ (id) actionWithAnimation: (CCAnimation*)anim
根据CCSpriteFrame
对象生成一个动画播放行为,持续的时间由帧数和间隔时间相乘算出。
+ (id) actionWithAnimation: (CCAnimation*)anim restoreOriginalFrame:(BOOL)b
b为yes时,当动画播放完毕后会切换回播放前显示的帧。
+ (id) actionWithDuration:(ccTime)duration animation: (CCAnimation*)anim restoreOriginalFrame:(BOOL)b
手动设置动画的播放时间,时间到动画才算结束。
cocos2d_x 播放帧动画主要流程
(1)创建CCSpriteFrame
数组,可以使用CCSpriteFrameCache
或者CCTextureCache
。
(2)通过帧序列创建CCAnimation
对象
(3)通过CCAnimation
对象和间隔时间创建CCAnimate
,生成一个持续性动作。
(4)使用精灵执行动作
动画创建方法(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,组成序列帧数组
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); --动画执行后还原初始状态
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
文件里保存了组成动画的相关信息,通过该类获取到plist
文件里的动画。
使用文件添加的方法只需将创建好的plist
文件添加到动画缓存里面,plist
文件里包含了序列帧的相关信息。再用动画缓存初始化Animation
实例,用Animate
实例来播放序列帧动画。
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>