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中把这些操作都封装完成。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值