cocos2d-x TestLua 之 CCAnimate

欢迎各位童鞋转载,转载请注明出处:http://blog.csdn.net/song_hui_xiang

作者新浪微博:http://weibo.com/u/3168848533

作者腾讯微博:http://t.qq.com/song_huixiang

local size = CCDirector:sharedDirector():getWinSize()
local layer = CCLayer:create()
local cache = nil
--加载plist文件
local function addPvrCczRes(plistName)
    local temp_name  = plistName..".plist"
    if cache then
        cache:addSpriteFramesWithFile(temp_name)
    else
        cache = CCSpriteFrameCache:sharedSpriteFrameCache()
        cache:addSpriteFramesWithFile(temp_name)
    end
end
--创建动画根据资源图片形式的不同,可以分为两类,一类为.png图片资源的形式,一类为.plist图片资源的形式,如下代码
local function ActionAnimate()
    --第一类为未打包的资源文件,.png形式
    local animation = CCAnimation:create()
    local name = nil
    for i=1,14 do
        if i<10 then
            name = "grossini_dance_0"..i..".png"
        else
            name = "grossini_dance_"..i..".png"
        end
        animation:addSpriteFrameWithFileName(name)
    end
    animation:setDelayPerUnit(2.8 / 14.0)  --每帧播放速率
    animation:setRestoreOriginalFrame(true) --做完动画还原成初始帧
    animation:setLoops(3)  --循环遍数,-1为永久循环
    local action = CCAnimate:create(animation)
    local sprite1 = CCSprite:create()
    sprite1:setPosition(ccp(size.width/4,size.height*0.5))
    layer:addChild(sprite1)
    sprite1:runAction(CCSequence:createWithTwoActions(action,action:reverse()))


    --第二类,图片资源为plist文件形式,.plist形式
    addPvrCczRes("XiangLongHand") --加载plist文件
    local array = CCArray:create()
    for i=1,20 do
        local name2 = "long_"..i..".png"
        local frame = cache:spriteFrameByName(name2)
        array:addObject(frame)
    end
    local animation2 = CCAnimation:createWithSpriteFrames(array,2/20)
    animation2:setLoops(4)
    local animate2 = CCAnimate:create(animation2)
    local sprite2 = CCSprite:create()
    sprite2:setPosition(ccp(2*size.width/4,size.height*0.5))
    layer:addChild(sprite2)
    sprite2:runAction(animate2)
    
    
    local animation3 = animation2:copy():autorelease() --复制这个animation
    animation3 = tolua.cast(animation3,"CCAnimation") --强制类型转换,getChildByTag()也是要进行类型转换
    local animate3 = CCAnimate:create(animation3)
    local sprite3 = CCSprite:create()
    sprite3:setPosition(ccp(3*size.width/4,size.height*0.5))
    layer:addChild(sprite3)
    sprite3:runAction(animate3)
end
ActionAnimate()

local scene = CCScene:create()
scene:addChild(layer)
CCDirector:sharedDirector():runWithScene(scene)

你可能感兴趣的:(cocos2d-x TestLua 之 CCAnimate)