Implement the MVC of lua in cocos2dx + lua
Create two classes. CGMainSceneManager is a singleton for MC and CGMainScene is for V. Implementation method similar to c ++ class
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 ()
-- Diagram of physical strength, gold coins, and silver coins
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 -- vertical interval
VerticalSpace = 0
VerticalOffset = 1, 450
Self. _ bgTableName =
{
"MainMap_1 ",
"MainMap_2 ",
"MainMap_3 ",
"MainMap_4 ",
"MainMap_5 ",
}
-- Floating panel
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,
}
-- Prompt box
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) -- add a level
Print ("this is checkPoint add function ")
Local function touchCheckPointEvent (sender, eventType)
If eventType = ccui. TouchEventType. began then
Print (sender: getParent (): getTag ())
Local tag = sender: getParent (): getTag ()
Elseif eventType = ccui. TouchEventType. moved then
Elseif eventType = ccui. TouchEventType. ended then
End
End
For I = 1, table. getn (bgTable) do -- add background
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, do
Local positionx = children [I + 1]: getPositionX ()
Local positiony = children [I + 1]: getPositionY ()
Local tag = children [I + 1]: getTag () -- get the tag Value
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) -- the current level gets several stars
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 () -- whether to pass through a series of action effects to be played by the current level
-- Action Test
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