Lua_场景节点事件

require "Cocos2d"
require "Cocos2dConstants"

-- for CCLuaEngine traceback
function __G__TRACKBACK__(msg)
    cclog("----------------------------------------")
    cclog("LUA ERROR: " .. tostring(msg) .. "\n")
    cclog(debug.traceback())
    cclog("----------------------------------------")
    return msg
end

local function main()
    collectgarbage("collect")
    --避免内存泄漏
    collectgarbage("setpause", 100)
    collectgarbage("setstepmul", 5000)
    
    --初始化导演
    local director = cc.Director:getInstance()
    local glview = director:getOpenGLView()
    if glview == nil then
        glview = cc.GLView:createWithRect("HelloLua", cc.rect(0, 0, 960, 640))
        director:setOpenGLView(glview)
    end

    glview:setDesignResolutionSize(960, 640, cc.ResolutionPolicy.SHOW_ALL)

    --显示帧率
    director:setDisplayStats(true)

    --设置帧率
    director:setAnimationInterval(1.0 / 60)

    --增加搜索路径
    cc.FileUtils:getInstance():addSearchPath("src")
    cc.FileUtils:getInstance():addSearchPath("res")
    
    --创建场景
    local scene = require("SettingScene")
    local settingScene = scene.create()
    
    --运行场景
    if cc.Director:getInstance():getRunningScene() then
        cc.Director:getInstance():replaceScene(settingScene)
    else
        cc.Director:getInstance():runWithScene(settingScene)
    end
end

xpcall(main, __G__TRACKBACK__)
require "Cocos2d"
require "Cocos2dConstants"

local size = cc.Director:getInstance():getWinSize()

local SettingScene = class("SettingScene",function()
    return cc.Scene:create()
end)

function SettingScene.create()
    local scene = SettingScene.new()
    scene:addChild(scene:createLayer())
    return scene
end

function SettingScene:ctor()
    cclog("SettingScene init")

    --场景节点事件处理
    local function onNodeEvent(event)
        if event == "enter" then
            self:onEnter()
        elseif event == "enterTransitionFinish" then
            self:onEnterTransitionFinish()
        elseif event == "exit" then
            self:onExit()
        elseif event == "exitTransitionStart" then
            self:onExitTransitionStart()
        elseif event == "cleanup" then
            self:cleanup()
        end
    end
    self:registerScriptHandler(onNodeEvent)
end

function SettingScene:onEnter()
    cclog("SettingScene onEnter")
end

function SettingScene:onEnterTransitionFinish()
    cclog("SettingScene onEnterTransitionFinish")
end

function SettingScene:onExit()
    cclog("SettingScene onExit")
end

function SettingScene:onExitTransitionStart()
    cclog("SettingScene onExitTransitionStart")
end

function SettingScene:cleanup()
    cclog("SettingScene cleanup")
end

return SettingScene


你可能感兴趣的:(Lua开发,Cocos2d-x,Lua,场景节点事件)