Cocos2d-x-lua实现简单的动画帧、读取CocoStudio文件

-- create a zombies<创建精灵帧动画>

local z_caches = CCSpriteFrameCache:sharedSpriteFrameCache()--加入缓存帧中

local z_path = CCFileUtils:sharedFileUtils():fullPathForFilename("zombies/BucketheadZombie_default.plist")

z_caches:addSpriteFramesWithFile(z_path)--添加plist文件

local z_sprite = CCSprite:createWithSpriteFrameName("BucketheadZombie1.png")--初始化一下精灵

z_sprite:setPosition(ccp(win_w/2-190, win_h/2-90))--设置精灵位置

local z_ccarray = CCArray:create();--创建数组存放精灵帧

z_ccarray:retain()

for i=1,15 do

local temp = string.format("BucketheadZombie%d.png",i)--读取拼接每帧动画名

local z_spriteFrame = z_caches:spriteFrameByName(temp)--从缓存中读取每帧

z_ccarray:addObject(z_spriteFrame)--加入到数组中

end

local z_animation = CCAnimation:createWithSpriteFrames(z_ccarray, 0.1)--将帧序列加入动画中

z_animation:setLoops(-1)--永久循环动作

local z_animate = CCAnimate:create(z_animation)--创建动画

z_sprite:runAction(z_animate)--运行动画

self:addChild(z_sprite)--加入层中

--创建一个 辣椒植物 

local p_path = CCFileUtils:sharedFileUtils():fullPathForFilename("zombies/Squash_default.plist")

z_caches:addSpriteFramesWithFile(p_path)

local p_sprite = CCSprite:createWithSpriteFrameName("Squash1(被拖移).tiff")

local p_animation = CCAnimation:create()

p_animation:setDelayPerUnit(0.2)

for k=1,17 do

local p_temp = string.format("Squash%d(被拖移).tiff",k)

local p_frame = z_caches:spriteFrameByName(p_temp)

p_animation:addSpriteFrame(p_frame)

end

p_sprite:runAction(CCRepeatForever:create(CCAnimate:create(p_animation)))

p_sprite:setPosition(ccp(win_w/2+200, win_h/2+90))

self:addChild(p_sprite)


-- 创建植物

local pea_path = CCFileUtils:sharedFileUtils():fullPathForFilename("zombies/Threepeater_default.plist")

z_caches:addSpriteFramesWithFile(pea_path)

local pea_sprite = CCSprite:createWithSpriteFrameName("Threepeater1(被拖移).tiff")

local pea_animation = CCAnimation:create()

pea_animation:setDelayPerUnit(0.2)

for i=1,16 do

local pea_temp = string.format("Threepeater%d(被拖移).tiff",i)

local pea_frame = z_caches:spriteFrameByName(pea_temp)

pea_animation:addSpriteFrame(pea_frame)

end

        pea_animation:setLoops(-1)

        local pea_animate = CCAnimate:create(pea_animation)

        pea_sprite:setPosition(ccp(win_w/2, win_h/2-100))

        pea_sprite:runAction(pea_animate)

        self:addChild(pea_sprite)


        -- 创建一个 Ball

        local  m_ball= Ball.new("dog.png")

        m_ball:setPosition(ccp(100, 100))

        m_ball.move()

        m_ball.setOriginal(ccp(90, 90))

        local moves = CCMoveTo:create(0.2, ccp(300, 100))

    local rotate = CCRotateBy:create(0.2, 360)

    local array = CCArray:create()

    array:addObject(moves)

    array:addObject(rotate)

    local spawn = CCSpawn:create(array) 

    m_ball:runAction(spawn)


        self:addChild(m_ball)


        -- 读取一个CocosStudio的文件Json

        local uiLayout = GUIReader:shareReader():widgetFromJsonFile("SampleUIAnimation.json")

        uiLayout:setPosition(ccp(win_w/2-90, win_h/2-100))

        self:addWidget(uiLayout)

你可能感兴趣的:(Cocos2d-x)