项目结构是这样子的:
主场景代码是这样子的:
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
百牛信息技术bainiu.ltd整理发布于博客园
新场景代码是这样子的:
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) end return AnotherScene
可是点击点击START之后进入的是一个黑色的新场景,DEBUG内容如下:
根本就没有打印AnotherScene.lua ctor()和onEnter()里面的提示内容。
经查验qucik cocos2dx源码,发现display.newScene("AnotherScene")新建了一个名为"AnotherScene"的CCScene,
而不是去取AnotherScene.lua,如下:
function display.newScene(name)local scene = CCSceneExtend.extend(CCScene:create())scene.name = name or "<unknown-scene>"return scene end
于是将item0的listener的代码如下:
print("Start touched")local AnotherScene = require("../scripts/app/scenes/AnotherScene")nexScene = AnotherScene:new();CCDirector:sharedDirector():replaceScene(CCTransitionFade:create(1, nexScene))
然后就正常了。
如下:
这也许就是脚本语言的便利与不便利之处了。