zoukankan      html  css  js  c++  java
  • 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
    复制代码

     百牛信息技术bainiu.ltd整理发布于博客园

    新场景代码是这样子的:

    复制代码
    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))

    然后就正常了。

    如下:

    这也许就是脚本语言的便利与不便利之处了。

  • 相关阅读:
    <s:property>的用法(jsp获取action中的值或者方法)
    struts2 Action获取表单数据
    form标签中id和name属性的区别
    button和submit区别
    hibernate could not resolve property
    Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyExce
    宏定义#define和内联函数inline的区别
    线程绑定cpu
    posix系统线程调度-设置线程优先级
    std::lock_guard和std::unique_lock的区别
  • 原文地址:https://www.cnblogs.com/bainiu/p/7592230.html
Copyright © 2011-2022 走看看