cocos场景切换时的loading界面实现

通过cc.Director的setNotificationNode方法,结合自定义的延时函数,实现在Cocos2d-x游戏中切换场景时显示Loading界面。首先将Loading界面放入NotificationNode,然后调用Utils.ShowTransScenePic()使其可见,接着利用schedule函数安排下一帧进入目标场景并隐藏NotificationNode,确保Loading界面先于新场景显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用cc.Director:getInstance():setNotificationNode(NotificationNode)来进行实现,并且编写一个简单的延时函数。

    global.NotificationNode = cc.Node:create()
    global.LoadingPic = cc.Sprite:create("ui/common/LoadingPic.png")
    LoadingPic:setAnchorPoint(cc.p(0,0))
    LoadingPic:setPosition(cc.p(0,0))
    LoadingPic:addChild(Utils.createLoadingNode())
    NotificationNode:addChild(LoadingPic)
    cc.Director:getInstance():setNotificationNode(NotificationNode)
    LoadingNode:setVisible(false)
    LoadingPic:setVisible(false)
    self.SwallowTouchlistener = Utils.addSwallowTouchListenerToNode(NotificationNode)  --屏蔽其他事件。
  Utils.ShowTransScenePic()
    Utils.delayExcute(0,function()
        g_AppInstance:enterScene(CONFIG.REVIEW_VERSION == true and "GameModeScene" or "TeamModeScene")
        Utils.HideTransScenePic()  
    end)

1.将自己做好的loading界面设置到NoNtificationode里面。这样在每次转换的界面的时候就会显示Node。

  Utils.ShowTransScenePic()
    Utils.delayExcute(0,function()
        g_AppInstance:enterScene(CONFIG.REVIEW_VERSION == true and "GameModeScene" or "TeamModeScene")
        Utils.HideTransScenePic()  
    end)
function Utils.delayExcute(delayTm, cb)
    local defaultSchedule = cc.Director:getInstance():getScheduler()
    local scheduleHandler = nil
    local function callback()
        if cb ~= nil then
            cb()
        end
        defaultSchedule:unscheduleScriptEntry(scheduleHandler)
        table.removebyvalue(delayScheduleHandlers, scheduleHandler)
    end
    scheduleHandler = defaultSchedule:scheduleScriptFunc(callback, delayTm, false)
    delayScheduleHandlers[#delayScheduleHandlers + 1] = scheduleHandler
    return scheduleHandler
end

2.Utils.ShowTransScenePic()这个函数是将之前自己设置在directer的notificationNode设置为显示状态。
3.然后自己利用schedule写一个延迟函数,在下一帧的时候进入你想进入的场景。并且影藏notificationNode。(这里为什么要在下一帧,因为Utils.ShowTransScenePic() 里面的setvisible(true) 只是将该图片加入到渲染队列,并不是立即显示在界面上来。而enterScene是会立即跳转界面并且显示,如果不利用延迟函数在下一帧进行enter,那么看到的效果就是进入场景然后显示了notificationNode。 而我们想要的效果是先显示notificationNode界面,scene成功加载在进行跳转。)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值