创建两个类,,CGMainSceneManager是单例,用于MC,CGMainScene 用于V。实现方式类似c++的类
local CGMainScene = class("CGMainScene",function()
return ZnBaseScene:create()
end)
function CGMainScene.create()
local scene = CGMainScene.new()
scene:init()
scene:registerScriptHandler(function(event)
if "enter" == event then
scene:enter()
elseif "exit" == event then
scene:exit()
end
end)
return scene
end
function CGMainScene:ctor()
self._visibleSize = cc.Director:getInstance():getVisibleSize()
self.origin = cc.Director:getInstance():getVisibleOrigin()
self.schedulerID = nil
end
function CGMainScene:init()
CGMainManager:instance():init()
CGMainManager:instance():createBgScrollView()
self:addChild(CGMainManager:instance()._scrollView)
CGMainManager:instance():addCheckPoint(CGMainManager:instance()._bgTableName)
local pointTable = {}
pointTable["level"] = 6
pointTable["state"] = 2
pointTable["starnum"] = 2
CGMainManager:instance():setStarandState(pointTable)
local buttonnode = cc.CSLoader:createNode("ButtonLayer.csb")
buttonnode:setPosition(VisibleRect.worldSpace(cc.p(self._visibleSize.width / 2,self._visibleSize.height)))
self:addChild(buttonnode,CGMainManager:instance()._panelZorder.kButtonZorder)
local function ButtonTouchEvent(sender,eventType)
if eventType == ccui.TouchEventType.began then
elseif eventType == ccui.TouchEventType.ended then
if sender:getTag() == 1 then
print("-----------shopButton----============")
elseif sender:getTag() ==2 then
print("-----------RoleButton----============")
elseif sender:getTag() == 3 then
print("-----------incubatorButton----============")
elseif sender:getTag() == 4 then
print("-----------lotteryButton----============")
elseif sender:getTag() == 5 then
print("-----------signButton----============")
elseif sender:getTag() == 6 then
print("-----------TaskButton----============")
elseif sender:getTag() == 7 then
print("-----------BagButton----============")
elseif sender:getTag() == 8 then
print("-----------button8----============")
elseif sender:getTag() == 9 then
print("-----------button9----============")
elseif sender:getTag() == 10 then
print("-----------button10----============")
end
end
end
local children = buttonnode:getChildren()
local len = table.getn(children)
for i=1,len do
children[i]:addTouchEventListener(ButtonTouchEvent)
end
-- CGMainManager:instance():setOffset()
--关于体力,金币,银币图
CGMainManager:instance():setPanelEvent()
local function IconTouchEvent(sender,eventType)
if eventType == ccui.TouchEventType.began then
elseif eventType == ccui.TouchEventType.ended then
if sender:getTag() == 1 then
print("icon energy button is touch ")
elseif sender:getTag() == 2 then
print("icon silver button is touch")
elseif sender:getTag() == 3 then
print("icon golden button is touch")
end
end
end
CGMainManager:instance()._energyIcon:addTouchEventListener(IconTouchEvent)
CGMainManager:instance()._goldenIcon:addTouchEventListener(IconTouchEvent)
CGMainManager:instance()._silverIcon:addTouchEventListener(IconTouchEvent)
CGMainManager:instance()._panelinfo:setPosition(VisibleRect.worldSpace(cc.p(self._visibleSize.width * 0.15,0.1 * self._visibleSize.height)))
self:addChild(CGMainManager:instance()._panelinfo,CGMainManager:instance()._panelZorder.kPanelZorder)
CGMainManager:instance():setPanelInfo()
--CGMainManager:instance():playEffect()
end
function CGMainScene:enter()
--print("CGMainScene:enter")
end
function CGMainScene:exit()
--print("CGMainScene:exit")
end
return CGMainScene
local instance = nil require("CGMainScene") CGMainManager = class("CGMainManager",function() return ZnBaseHandler:create() end) function CGMainManager:instance() if instance ~= nil then return instance end instance = CGMainManager.new() return instance end function CGMainManager:reset() if instance ~= nil then instance = false end end function CGMainManager:ctor() self._winSize = cc.Director:getInstance():getWinSize() self._visibleSize = cc.Director:getInstance():getVisibleSize() end function CGMainManager:init() self._checkPointTable = {} self._scrollView = nil imageViewTag = 100 self._imageView = cc.Sprite:create("guanka1.png") -- image for test verticalTotal = 0 --竖直方向上的间隔 verticalSpace = 0 verticalOffset = 450 self._bgTableName = { "MainMap_1","MainMap_2","MainMap_3","MainMap_4","MainMap_5",} --浮动面板 self._panelinfo = nil self._curEnergyLabel = nil self._totalEnergyLabel = nil self._goldenLabel = nil self._silverLabel = nil self._energyIcon = nil self._goldenIcon = nil self._silverIcon = nil self._panelZorder = { kPanelZorder = 1,kButtonZorder = 2,} --提示框 alertView = nil cdTime = 10*60*60 --for test level = 20 end function CGMainManager:createBgScrollView() print("create bg scrollView ") self._scrollView = cc.ScrollView:create() local function scrollView1DidScroll() end local function scrollView1DidZoom() end if nil ~= self._scrollView then self._scrollView:setViewSize(cc.size(self._winSize.width,self._winSize.height)) self._scrollView:setPosition(cc.p(0,0)) self._scrollView:setScale(1.0) self._scrollView:ignoreAnchorPointForPosition(true) self._scrollView:setContentSize(cc.size(self._winSize.width,self._winSize.height * #self._bgTableName -verticalOffset * 2)) self._scrollView:setDirection(cc.SCROLLVIEW_DIRECTION_VERTICAL ) self._scrollView:setBounceable(true) self._scrollView:setDelegate() self._scrollView:registerScriptHandler(scrollView1DidScroll,cc.SCROLLVIEW_SCRIPT_SCROLL) end end function CGMainManager:addCheckPoint(bgTable) --添加关卡 print("this is checkPoint add function ") local function touchCheckPointEvent(sender,eventType) if eventType == ccui.TouchEventType.began then print(sender:getParent():getParent():getTag()) local tag = sender:getParent():getParent():getTag() elseif eventType == ccui.TouchEventType.moved then elseif eventType == ccui.TouchEventType.ended then end end for i=1,table.getn(bgTable) do --添加背景 local name = bgTable[i] .. ".csb" local bg = cc.CSLoader:createNode(name) local bgsprite = bg:getChildByName(bgTable[i]) local children = bgsprite:getChildren() local len = table.getn(children) for i = 0,len - 1,1 do local positionx = children[i + 1]:getPositionX() local positiony = children[i + 1]:getPositionY() local tag = children[i + 1]:getTag() --拿到tag值 local checkpoint = cc.CSLoader:createNode("Checkpoint.csb") checkpoint:setPosition(cc.p(positionx,positiony)) table.insert(self._checkPointTable,checkpoint) bg:addChild(checkpoint,tag) local pointbg = checkpoint:getChildByName("Checkpoint_Bg") local button = pointbg:getChildByName("Point_Button") button:addTouchEventListener(touchCheckPointEvent) local star1 = pointbg:getChildByName("PointStar_1") local star2 = pointbg:getChildByName("PointStar_2") local star3 = pointbg:getChildByName("PointStar_3") local grayStar1 = pointbg:getChildByName("PointStar_Close1") local grayStar2 = pointbg:getChildByName("PointStar_Close2") local grayStar3 = pointbg:getChildByName("PointStar_Close3") local stateclose = pointbg:getChildByName("Point_Close") local stateopen = pointbg:getChildByName("Point_Open") local stateok = pointbg:getChildByName("Point_Ok") local imagepoint = pointbg:getChildByName("Sprite_image") local silverandgoldenPoint = pointbg:getChildByName("pointSilverOrGolden") silverandgoldenPoint:setVisible(false) local pointNum = pointbg:getChildByName("Point_Num") pointNum:setString(string.format("%s",tag)) self._checkPointTable[tag] = {} self._checkPointTable[tag]["starTable"] = {} self._checkPointTable[tag]["grayStarTable"] = {} self._checkPointTable[tag]["checkpointState"] = {} self._checkPointTable[tag]["starnum"] = {} self._checkPointTable[tag]["imagepoint"] = {} self._checkPointTable[tag]["pointbg"] = {} table.insert(self._checkPointTable[tag]["starTable"],star1) table.insert(self._checkPointTable[tag]["starTable"],star2) table.insert(self._checkPointTable[tag]["starTable"],star3) table.insert(self._checkPointTable[tag]["grayStarTable"],grayStar1) table.insert(self._checkPointTable[tag]["grayStarTable"],grayStar2) table.insert(self._checkPointTable[tag]["grayStarTable"],grayStar3) table.insert(self._checkPointTable[tag]["checkpointState"],stateclose) table.insert(self._checkPointTable[tag]["checkpointState"],stateopen) table.insert(self._checkPointTable[tag]["checkpointState"],stateok) table.insert(self._checkPointTable[tag]["starnum"],0) --当前等级获得几颗星 table.insert(self._checkPointTable[tag]["imagepoint"],imagepoint) table.insert(self._checkPointTable[tag]["pointbg"],pointbg) end bg:setPosition(VisibleRect.worldSpace(cc.p(0,self._visibleSize.height -(i - 1) * self._visibleSize.height + verticalOffset))) self._scrollView:getContainer():addChild(bg,0) self._scrollView:updateInset() end end function CGMainManager:setPanelEvent() self._panelinfo = cc.CSLoader:createNode("TopBar.csb") local energybg = self._panelinfo:getChildByName("ico_energy_bg") local goldenbg = self._panelinfo:getChildByName("ico_gold_bg") local silverbg = self._panelinfo:getChildByName("ico_silver_bg") self._energyIcon = energybg:getChildByName("ico_energy") self._goldenIcon = goldenbg:getChildByName("ico_gold") self._silverIcon = silverbg:getChildByName("ico_silver") self._curEnergyLabel = energybg:getChildByName("energy_Num_Left") self._totalEnergyLabel = energybg:getChildByName("energy_Num_Right") self._goldenLabel = goldenbg:getChildByName("gold_Num") self._silverLabel = silverbg:getChildByName("silver_Num") end function CGMainManager:setPanelInfo() local curEnergy = string.format("%s",300) local totalEnergy = string.format("%s",500) local golden = string.format("%s",1000) local silver = string.format("%s",2000) self._curEnergyLabel:setString(curEnergy) self._totalEnergyLabel:setString(totalEnergy) self._goldenLabel:setString(golden) self._silverLabel:setString(silver) end function CGMainManager:showAlertView(flag) end function CGMainManager:setStarandState(PointTable) -- start init local level = PointTable["level"] local state = PointTable["state"] local starnum = PointTable["starnum"] if self._imageView ~= nil then local pointx = self._checkPointTable[level]["imagepoint"][1]:getPositionX() local pointy = self._checkPointTable[level]["imagepoint"][1]:getPositionY() self._imageView:setPosition(cc.p(pointx,pointy)) self._checkPointTable[level]["pointbg"][1]:addChild(self._imageView) for i=1,level do --star for j=1,starnum do self._checkPointTable[level]["grayStarTable"][j]:setVisible(false) end --state if state ~= 1 then self._checkPointTable[level]["checkpointState"][1]:setVisible(false) self._checkPointTable[level]["checkpointState"][state]:setVisible(true) elseif state == 1 then end end end end function CGMainManager:setOffset() self._scrollView:setContentOffset(cc.p(0,-verticalSpace)) end function CGMainManager:playEffect() -- 当前关卡要播放的一系列动作特效 是否通关 --动作测试 local tag = 6 local silverIconx = self._silverIcon:getPositionX() local silverIcony = self._silverIcon:getPositionY() local goldenIconx = self._goldenIcon:getPositionX() local goldenIcony = self._goldenIcon:getPositionY() local pointsilverIcon = self._silverIcon:convertToWorldSpace(cc.p(silverIconx,silverIcony)) local pointgoldenIcon = self._goldenIcon:convertToWorldSpace(cc.p(goldenIconx,goldenIcony)) local silverMove = cc.MoveTo:create(2,pointsilverIcon) local goldenMove = cc.MoveTo:create(2,pointgoldenIcon) local silver = cc.Sprite:create("guanka1.png") local golden = cc.Sprite:create("guanka1.png") silver:setPosition(cc.p(10,10)) golden:setPosition(cc.p(10,10)) --[[ local silverx = silver:getPositionX() local silvery = silver:getPositionY() local goldenx = golden:getPositionX() local goldeny = golden:getPositionY() local positonsilver = convertToWorldSpace(cc.p(silverx,silvery)) local positiongolden = convertToWorldSpace(cc.p(goldenx,goldeny)) ]] self._checkPointTable[tag]["pointbg"][1]:addChild(silver,100) self._checkPointTable[tag]["pointbg"][1]:addChild(golden,100) silver:runAction(silverMove) golden:runAction(goldenMove) end return CGMainManager
原文链接:https://www.f2er.com/cocos2dx/344111.html