Quick-cocos2d-x 中進度條與資源加載

爲便於看客實際操作和理解,直接上例子

demo中要調用父類中的函數和表,固先列出父類中響應的函數和表

local resPathes = 
{
	s_pPathB1             = "Images/b1.png",
	s_pPathB2             = "Images/b2.png",
	s_pPathR1             = "Images/r1.png",
	s_pPathR2             = "Images/r2.png",
	s_pPathF1             = "Images/f1.png",
	s_pPathF2             = "Images/f2.png",

	s_arialPath            = "fonts/arial.ttf"
}
ArmatureTestLayer.resPathes = resPathes

local ArmatureTestIndex =
{
    TEST_ASYNCHRONOUS_LOADING     = 1,
    TEST_DIRECT_LOADING           = 2,
    TEST_COCOSTUDIO_WITH_SKELETON = 3,
    TEST_DRAGON_BONES_2_0 = 4,
    TEST_PERFORMANCE = 5,
    TEST_CHANGE_ZORDER = 6,
    TEST_ANIMATION_EVENT = 7,
    TEST_FRAME_EVENT     = 8,
    TEST_PARTICLE_DISPLAY = 9,
    TEST_USE_DIFFERENT_PICTURE = 10,
    TEST_ANCHORPOINT = 11,
    TEST_ARMATURE_NESTING = 12,
    TEST_ARMATURE_NESTING_2 = 13,
}
function ArmatureTestLayer.title(idx)
    if ArmatureTestIndex.TEST_ASYNCHRONOUS_LOADING == idx then
        return "Test Asynchronous Loading"
    elseif ArmatureTestIndex.TEST_DIRECT_LOADING   == idx then
        return "Test Direct Loading"
    elseif ArmatureTestIndex.TEST_COCOSTUDIO_WITH_SKELETON == idx then
        return "Test Export From CocoStudio With Skeleton Effect"
    elseif ArmatureTestIndex.TEST_DRAGON_BONES_2_0 == idx then
        return "Test Export From DragonBones version 2.0"
    elseif ArmatureTestIndex.TEST_PERFORMANCE == idx then
        return "Test Performance"
    elseif ArmatureTestIndex.TEST_CHANGE_ZORDER == idx then
        return "Test Change ZOrder Of Different Armature"
    elseif ArmatureTestIndex.TEST_ANIMATION_EVENT == idx then
        return "Test Armature Animation Event"
    elseif ArmatureTestIndex.TEST_FRAME_EVENT == idx then
        return "Test Frame Event"
    elseif ArmatureTestIndex.TEST_PARTICLE_DISPLAY == idx then
        return "Test Particle Display"
    elseif ArmatureTestIndex.TEST_USE_DIFFERENT_PICTURE == idx then
        return "Test One Armature Use Different Picture"
    elseif ArmatureTestIndex.TEST_ANCHORPOINT == idx then
        return "Test Set AnchorPoint"
    elseif ArmatureTestIndex.TEST_ARMATURE_NESTING == idx then
        return "Test Armature Nesting"
    elseif ArmatureTestIndex.TEST_ARMATURE_NESTING_2 == idx then
        return "Test Armature Nesting 2"
    end
end

function ArmatureTestLayer.subTitle(idx)
    if ArmatureTestIndex.TEST_ASYNCHRONOUS_LOADING == idx then
        return "current percent :"
    elseif ArmatureTestIndex.TEST_PERFORMANCE == idx then
        return "Current Armature Count : "
    elseif ArmatureTestIndex.TEST_PARTICLE_DISPLAY == idx then
        return "Touch to change animation"
    elseif ArmatureTestIndex.TEST_USE_DIFFERENT_PICTURE == idx then
        return "weapon and armature are in different picture"
    elseif ArmatureTestIndex.TEST_ARMATURE_NESTING_2 == idx then
        return "Move to a mount and press the ChangeMount Button."
    else
        return ""
    end
end

目標類調用父類的函數已經列出,下面是目標代碼

local ArmatureTestLayer = require("layers.ArmatureTestLayer")
local TestAsynchronousLoading = class("TestAsynchronousLoading", ArmatureTestLayer)

function TestAsynchronousLoading:ctor()
	TestAsynchronousLoading.super.ctor(self)

end

function TestAsynchronousLoading:onEnter()
    --設置在父類中定義的三個按鈕可以點擊
    self.prevButton:setButtonEnabled(false)
    self.restartButton:setButtonEnabled(false)
    self.nextButton:setButtonEnabled(false)

    --設置標題   self.title(1)  獲取父類標題表中對應的字符串 self.resPathes.s_arialPath 獲取字體文件
    local title = cc.Label:createWithTTF(self.title(1), self.resPathes.s_arialPath, 18)
    title:setColor(cc.c3b(0,0,0))
    self:addChild(title, 1, 10000)
    title:setAnchorPoint(cc.p(0.5, 0.5))
    title:setPosition( display.cx, display.top - 30 )

    --設置顯示進度的title
    local subInfo = self.subTitle(1)
    if "" ~= subInfo then
        subInfo  = subInfo .. 0.0
        local subTitle = cc.Label:createWithTTF(subInfo, self.resPathes.s_arialPath, 18)
        subTitle:setColor(cc.c3b(0,0,0))
        self:addChild(subTitle, 1, 10001)
        subTitle:setAnchorPoint(cc.p(0.5, 0.5))
        subTitle:setPosition( display.cx, display.top - 60 )
    end

    --創建進度條
    local timer = cc.ProgressTimer:create(cc.Sprite:create("loading.png"))
    timer:setPosition(display.cx, display.cy)
    timer:setBarChangeRate(cc.p(1,0))
    timer:setType(display.PROGRESS_TIMER_BAR)
    timer:setMidpoint(cc.p(0,0.5))--基準點
    timer:setPercentage(0)
    self:addChild(timer, 50, 10002)



    local function dataLoaded(percent)
        --獲取進度條
        local timer1 = self:getChildByTag(10002)
        --獲取進度顯示title
        local label = self:getChildByTag(10001)
        if nil ~= label then
            --設置進度條進度
            timer1:setPercentage(percent*100)
            local subInfo = self.subTitle(1) .. (percent * 100)
            label:setString(subInfo)
        end
        --加載完成,界面上的按鈕纔可以點擊
        if percent >= 1 and self.prevButton and self.restartButton and self.nextButton then
            self.prevButton:setButtonEnabled(true)
            self.restartButton:setButtonEnabled(true)
            self.nextButton:setButtonEnabled(true)
        end
    end

    local manager = ccs.ArmatureDataManager:getInstance()
	
	--addArmatureFileInfoAsync適用異步加載骨骼動畫資源
	--addImageAsync適用異步加載圖片資源
manager:addArmatureFileInfoAsync("armature/knight.png", "armature/knight.plist", "armature/knight.xml", dataLoaded) manager:addArmatureFileInfoAsync("armature/weapon.png", "armature/weapon.plist", "armature/weapon.xml", dataLoaded) manager:addArmatureFileInfoAsync("armature/robot.png", "armature/robot.plist", "armature/robot.xml", dataLoaded) manager:addArmatureFileInfoAsync("armature/cyborg.png", "armature/cyborg.plist", "armature/cyborg.xml", dataLoaded) manager:addArmatureFileInfoAsync("armature/Dragon.png", "armature/Dragon.plist", "armature/Dragon.xml", dataLoaded) manager:addArmatureFileInfoAsync("armature/Cowboy.ExportJson", dataLoaded) manager:addArmatureFileInfoAsync("armature/hero.ExportJson", dataLoaded) manager:addArmatureFileInfoAsync("armature/horse.ExportJson", dataLoaded) manager:addArmatureFileInfoAsync("armature/bear.ExportJson", dataLoaded) manager:addArmatureFileInfoAsync("armature/HeroAnimation.ExportJson", dataLoaded)endreturn TestAsynchronousLoading


對比cocos2d-x中的資源加載,Quick中在每次加載完資源之後不需要手動更新百分比數 ( cocos2d-x中  float percent=100*res_count/15.0;//一共需要加載15次(因爲有15個文件))  Quick中把這些操作都封裝完成。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章