quick-cocos2dx AppBase

来源:互联网 发布:广联达电力计价软件 编辑:程序博客网 时间:2024/04/26 16:38
local AppBase = class("AppBase")AppBase.APP_ENTER_BACKGROUND_EVENT = "APP_ENTER_BACKGROUND_EVENT"AppBase.APP_ENTER_FOREGROUND_EVENT = "APP_ENTER_FOREGROUND_EVENT"function AppBase:ctor(appName, packageRoot)    -- 使App的对象能够附加组件。    cc.GameObject.extend(self)    -- 添加组件EventProtocol,将其中的函数导出到app上。    self:addComponent("components.behavior.EventProtocol"):exportMethods()    self.name = appName    self.packageRoot = packageRoot or "app"    -- 在通知中心注册进入前台、后台的响应函数    local notificationCenter = CCNotificationCenter:sharedNotificationCenter()    notificationCenter:registerScriptObserver(nil, handler(self, self.onEnterBackground), "APP_ENTER_BACKGROUND_EVENT")    notificationCenter:registerScriptObserver(nil, handler(self, self.onEnterForeground), "APP_ENTER_FOREGROUND_EVENT")    self.snapshots_ = {}    -- set global app    -- 若想使用此app,可用全局变量 “app”    app = selfendfunction AppBase:run()end-- 结束游戏function AppBase:exit()    CCDirector:sharedDirector():endToLua()    os.exit()end-- 进入某场景,实际调用display.replaceScenefunction AppBase:enterScene(sceneName, args, transitionType, time, more)    local scenePackageName = self. packageRoot .. ".scenes." .. sceneName    local sceneClass = require(scenePackageName)    local scene = sceneClass.new(unpack(totable(args)))    display.replaceScene(scene, transitionType, time, more)end-- 创建viewfunction AppBase:createView(viewName, ...)    local viewPackageName = self. packageRoot .. ".views." .. viewName    local viewClass = require(viewPackageName)    return viewClass.new(...)endfunction AppBase:makeLuaVMSnapshot()    self.snapshots_[#self.snapshots_ + 1] = CCLuaStackSnapshot()    while #self.snapshots_ > 2 do        table.remove(self.snapshots_, 1)    end    return selfendfunction AppBase:checkLuaVMLeaks()    assert(#self.snapshots_ >= 2, "AppBase:checkLuaVMLeaks() - need least 2 snapshots")    local s1 = self.snapshots_[1]    local s2 = self.snapshots_[2]    for k, v in pairs(s2) do        if s1[k] == nil then            print(k, v)        end    end    return selfendfunction AppBase:onEnterBackground()    self:dispatchEvent({name = AppBase.APP_ENTER_BACKGROUND_EVENT})endfunction AppBase:onEnterForeground()    self:dispatchEvent({name = AppBase.APP_ENTER_FOREGROUND_EVENT})endreturn AppBase

0 0
原创粉丝点击