创建两个类,,CGMainSceneManager是单例,用于MC,CGMainScene 用于V。实现方式类似c++的类
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,0,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
原文地址:http://blog.csdn.net/zhuanzhuwanmei/article/details/44116861