您的位置:首页 > 移动开发 > Cocos引擎

quick-cocos2dx AppBase

2013-12-13 16:38 267 查看
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 = self
end

function AppBase:run()
end

-- 结束游戏
function AppBase:exit()
CCDirector:sharedDirector():endToLua()
os.exit()
end

-- 进入某场景,实际调用display.replaceScene
function 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

-- 创建view
function AppBase:createView(viewName, ...)
local viewPackageName = self. packageRoot .. ".views." .. viewName
local viewClass = require(viewPackageName)
return viewClass.new(...)
end

function AppBase:makeLuaVMSnapshot()
self.snapshots_[#self.snapshots_ + 1] = CCLuaStackSnapshot()
while #self.snapshots_ > 2 do
table.remove(self.snapshots_, 1)
end

return self
end

function 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 self
end

function AppBase:onEnterBackground()
self:dispatchEvent({name = AppBase.APP_ENTER_BACKGROUND_EVENT})
end

function AppBase:onEnterForeground()
self:dispatchEvent({name = AppBase.APP_ENTER_FOREGROUND_EVENT})
end

return AppBase
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: