Quick Cocos2dx 场景转换问题
生活随笔
收集整理的這篇文章主要介紹了
Quick Cocos2dx 场景转换问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
項目結構是這樣子的:
主場景代碼是這樣子的:
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))然后就正常了。
如下:
?
這也許就是腳本語言的便利與不便利之處了。
轉載于:https://www.cnblogs.com/bainiu/p/7592230.html
總結
以上是生活随笔為你收集整理的Quick Cocos2dx 场景转换问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 拉普拉斯金字塔融合原理浅析
- 下一篇: webm是什么格式?