cocos2dx-3.16 异步加载图片 显示进度条

--------------------------------
-- 登陆界面
--------------------------------
local UILoading = class("UILoading", UIBase)

local tbPng = {

    "map/landform/huacong",
    "map/landform/kucaodi",
    "map/landform/mudidimao1",
    "map/landform/qincaodi",
    "map/landform/shilin1",
    "map/landform/shilin2",
    "map/landform/shilin3",
    "map/landform/shilin4",
    "map/landform/tu1dimao1",
    "map/landform/tu2dimao1",
    "map/landform/xuanya1",

    "map/small_obj/diaoxiang",
    "map/small_obj/jianzhu",
    "map/small_obj/mudijianzhu1",
    "map/small_obj/mudijianzhu2",
    "map/small_obj/mudishanshi",
    "map/small_obj/mudiwujian",
    "map/small_obj/mudiwujian2",
    "map/small_obj/mudizhibei",
    "map/small_obj/shanshi",
    "map/small_obj/shumu",
    "map/small_obj/tu1jianzhu",
    "map/small_obj/tu1wujian",
    "map/small_obj/tu1zhibei",
    "map/small_obj/weilan",
    "map/small_obj/weiqiang",
    "map/small_obj/wujian",
    "map/small_obj/zhalan",
    "map/small_obj/zhibei",

    "map/style_ground/shourenyingdi1",
    "map/style_ground/mudifengge1",
    "map/style_ground/qianzhenshilin1",
}
    

function UILoading:ctor()
    
end

function UILoading:create()
    return UIBase:create(UILoading, "ui/UILogin/UILoading.ExportJson")
end

function UILoading:initUI()

    self.m_Image_Loading = g_seekWidgetByName(self._root, "m_Image_Loading")
    self.m_ProgressBar_Load = g_seekWidgetByName(self._root, "m_ProgressBar_Load")
    
    self.m_Label_Loading = g_seekWidgetByName(self._root, "m_Label_Loading")

    local  scale = cc.ScaleBy:create(0.3, 2)
    local  scale_back = scale:reverse()
    local  seq = cc.Sequence:create(scale, scale_back)
    self.m_Label_Loading:runAction(cc.RepeatForever:create(seq))

    print("UILoading 构造函数")

    self.dtNum = 0

    local ret = cc.Layer:create()
    self:addChild(ret)

    local function callback(args, texture)
        print("加载png完毕", args)
        print("进度条 %", args/#tbPng * 100)

        self.m_ProgressBar_Load:setPercent(args/#tbPng * 100) --nexp当前值 upexp最大值 参数 1 ~ 100之间

        if args == #tbPng then
            --self.m_Image_Loading:hide()
            print("进度条全部执行完毕")
            DlgManager.deleteDlgByName("UILoading")
            DlgManager.pushDlg("GameLoginView")
            self.isClose = true
        end

        --print("Image loaded:..." .. i)-- %p", tex)
    end

    local function loadImages(dt)

        for i, pngName in ipairs(tbPng) do
            --cc.SpriteFrameCache:getInstance():addSpriteFrames(pngName .. ".plist")
            --display.loadImage(pngName .. ".png", handler(i, callback))
            cc.Director:getInstance():getTextureCache():addImageAsync(pngName .. ".png", handler(i, callback))
        end

        ret:unscheduleUpdate()
    end

    local function onNodeEvent(event)
        if event == "enter" then
            ret:scheduleUpdateWithPriorityLua(loadImages,0)
        elseif event == "exit" then
            ret:unscheduleUpdate()
            --cc.Director:getInstance():getTextureCache():removeAllTextures()
        end
    end

    ret:registerScriptHandler(onNodeEvent)

--[[
    self:scheduleUpdateWithPriorityLua(function(dt) self:onUpDate(dt) end, 1)
    G_GameScene.GetInstance():registerUpdate("UILoading")
]]
    -- 初始化逻辑
    self:InitDeal()
end

function UILoading:InitDeal()
    printf("UILoading:InitDeal suffcul")
end

return UILoading

你可能感兴趣的:(cocos2dx-3.16 异步加载图片 显示进度条)