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)
end

function MainScene:onEnter()
    self.layer:setTouchEnabled(true)
end

function MainScene:onTouch(event, x, y)
    print(event)
end

function MainScene:onExit()
end

return MainScene

 

新場景代碼是這樣子的:

local AnotherScene = class("AnotherScene", function()
    return display.newScene("AnotherScene")
end)

function AnotherScene:ctor()
    print("Constructor of AnotherScene")
end

function 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))

然后就正常了。

如下:

 

這也許就是腳本語言的便利與不便利之處了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM