cocos2d-x lua 帧动画

作者: 亮亮同学 | 来源:发表于2017-12-15 18:00 被阅读9次

    cocos2d-x技术群新群:117871561
    c++技术交流群:593010226

    1,为了在游戏中流畅的播放帧动画 在进入场景时先缓存动画帧 。当播放动画时 就不用每次都要重新加载动画帧 了,但是要在退出场景时要remove掉,减少内存消耗

    --注册layer的 进入 退出事件
    --进入场景时触发此函数
        local function onNodeEvent(event)
            if event == "exit" then
                self:onExit()
            elseif event == "enter" then
                self:onEnter()
            end
        end
        self:registerScriptHandler(onNodeEvent)
    
    --在进入场景时缓存动画帧
    function GameViewLayer:onEnter()
        print("... GameViewLayer:onEnter ...")
        for i=0,5 do
            local name = string.format("operate_action/buhua_%d.png",i)
            local sp = cc.Sprite:create(name)
            cc.SpriteFrameCache:getInstance():addSpriteFrame(sp:getSpriteFrame(),name)
        end
    end
    
    
    --播放动画帧
    function GameViewLayer:showBuxi(viewId)
         local frames = {}
      --读取缓存帧到 frames
        for i=1,5 do
            local name = string.format("operate_action/buhua_%d.png",i)
            frames[i] = 
            cc.SpriteFrameCache:getInstance():getSpriteFrame(name)
        end
              --创建Animation (帧table,播放间隔 )
        local animation = cc.Animation:createWithSpriteFrames(frames, 0.06)
              --创建Animate
        local animate = cc.Animate:create(animation)        
            display.newSprite(animation[1])
            :move(0, 90)
            :runAction(animate)
    end
      -- 退出场景时删除动画缓存帧
    function GameViewLayer:onExit()
        print("GameViewLayer onExit")
      
        for i=0,5 do
            local name = string.format("operate_action/buhua_%d.png",i)
            cc.SpriteFrameCache:getInstance():removeSpriteFrameByName(name)
        end
    
    end
    
    
    

    相关文章

      网友评论

        本文标题:cocos2d-x lua 帧动画

        本文链接:https://www.haomeiwen.com/subject/jcydwxtx.html