本文主要是介绍lua cocos2dx UIManager,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
仅供新手学习的UIManager
local scheduler = require(cc.PACKAGE_NAME .. ".scheduler")
local UIManager=class("UIManager")local instance=nil
local _root_view = nil
local _layer_map=nil
local _updateLayer_map=nil
local _layer_zorder=nil
function UIManager:ctor()_layer_map={}_updateLayer_map={}_layer_zorder=0
endfunction UIManager:getInstance()if instance==nil theninstance=UIManager.new()endreturn instance
end
--创建一个UILayer
function UIManager:addLayer(layer_tag,ZOrder,layer_class,isupdate)local _layer_data={}_layer_data.layer_tag=layer_tag_layer_data.class= layer_class:new()self:setZOrad(ZOrder)_layer_data.ZOrder=ZOrderif isupdate then_updateLayer_map[layer_tag]=_layer_dataelse_layer_map[layer_tag]=_layer_dataend_layer_data.class:setLocalZOrder(ZOrder)local csb_node=self:readCsbNode(layer_class)if csb_node thencsb_node:addTo(_layer_data.class)endif _layer_data.class:onCreate() then_layer_data.class:addTo(_root_view)else_layer_data.class:addTo(_root_view)endNewHandMgr:getInstance():addUI(layer_tag,layer_class)endfunction UIManager:readCsbNode(layer_class)if layer_class.FILE_NAME thenlocal path=layer_class.FILE_NAMElocal csb_node=cc.CSLoader:createNode(path)layer_class.resource_node=csb_nodereturn csb_nodeend
end--注册到UILayer上
function UIManager:RegisterGameLayer(root_view)_root_view = root_view
endfunction UIManager:getLayerByTag(layer_tag)if not _layer_map thenreturnend if _layer_map[layer_tag]~=nil thenreturn _layer_map[layer_tag].classelseif _updateLayer_map[layer_tag]~=nil thenreturn _updateLayer_map[layer_tag].classelsereturnend
end
--通过layer_tag销毁一个层
function UIManager:destorylayer(layer_tag)if not _layer_map thenreturnend if _layer_map[layer_tag]~=nil then_layer_map[layer_tag].class:destory()_layer_map[layer_tag].class:removeFromParent()_layer_map[layer_tag]=nilelseif _updateLayer_map[layer_tag]~=nil then_updateLayer_map[layer_tag].class:destory()_updateLayer_map[layer_tag].class:removeFromParent()_updateLayer_map[layer_tag]=nilelsereturnendend
--设置layer的Zorder
function UIManager:setZOrad(ZOrder)if ZOrder==BaseLayerDef.ZORDER_TAG.NORMAL_ZORDER then_layer_zorder=_layer_zorder+1ZOrder=BaseLayerDef.ZORDER_TAG.NORMAL_ZORDER+_layer_zorderreturn ZOrder elsereturn ZOrderend
end--更新数据
function UIManager:update(dt)for layer_tag,value in pairs(_updateLayer_map) do_updateLayer_map[layer_tag].class:update(dt)end
endfunction UIManager:onRelease()-- for layer_tag,class in pairs(_layer_map) do-- self:destorylayer(layer_tag)-- end-- for layer_tag,class in pairs(_updateLayer_map) do-- self:destorylayer(layer_tag)-- end_layer_map={}_updateLayer_map={}
endreturn UIManager
这篇关于lua cocos2dx UIManager的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!