The project structure is like this:
The main scenario code is like this:
local MainScene = class("MainScene", function() return display.newScene("MainScene")end)function MainScene:ctor() self.layer = display.newLayer(); self:addChild(self.layer) self.item0 = ui.newTTFLabelMenuItem({text = "START", size = 64, align = ui.TEXT_ALIGN_CENTER, x = display.cx, y = display.cy + 50, listener = function() print("Start touched") nexScene = display.newScene("AnotherScene"); CCDirector:sharedDirector():replaceScene(CCTransitionFade:create(1, nexScene)) end}) self.item1 = ui.newTTFLabelMenuItem({text = "ABOUT", size = 64, align = ui.TEXT_ALIGN_CENTER, x=display.cx, y=display.cy, listener = function() print("About touched") end}) self.item2 = ui.newTTFLabelMenuItem({text = "EXIT", size = 64, align = ui.TEXT_ALIGN_CENTER, x=display.cx, y=display.cy-50, listener = function() print("Exit touched") game.exit() end}) self.menu = ui.newMenu({self.item0,self.item1,self.item2}) self.layer:addChild(self.menu)endfunction MainScene:onEnter() self.layer:setTouchEnabled(true)endfunction MainScene:onTouch(event, x, y) print(event)endfunction MainScene:onExit()endreturn MainScene
The code for the new scenario is like this:
local AnotherScene = class("AnotherScene", function() return display.newScene("AnotherScene")end)function AnotherScene:ctor() print("Constructor of AnotherScene")endfunction AnotherScene:onEnter() print("Custom AnotherScene:onEnter") ui.newTTFLabel({text = "AnotherScene", size = 64, align = ui.TEXT_ALIGN_CENTER}) :pos(display.cx, display.cy) :addTo(self)endreturn AnotherScene
However, after clicking start, a new black scenario is displayed. The debug content is as follows:
No printing at allAnotherscene. Lua Ctor ()AndOnenter ()Content.
After checking the source code of qucik cocos2dx, we found thatDisplay. newscene ("anotherscene ")A new name named"Anotherscene"OfCcscene,
Instead of retrievingAnotherscene. Lua, As follows:
function display.newScene(name) local scene = CCSceneExtend.extend(CCScene:create()) scene.name = name or "<unknown-scene>" return sceneend
The listener code of item0 is as follows:
print("Start touched") local AnotherScene = require("../scripts/app/scenes/AnotherScene") nexScene = AnotherScene:new(); CCDirector:sharedDirector():replaceScene(CCTransitionFade:create(1, nexScene))
Then it is normal.
As follows:
This may be the convenience and inconvenience of the script language.