if (engine->executeScriptFile(“src/main.lua”)) {
return false;
}
return true;
}
// This function will be called when the app is inactive. When comes a phone call,it’s be invoked too
// 当应用程序将要进入后台时,会调用这个方法
void AppDelegate::applicationDidEnterBackground()
{
Director::getInstance()->stopAnimation();
SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
}
// this function will be called when the app is active again
// 该方法与applicationDidEnterBackground() 成对出现,在应用程序回到前台时被调用
void AppDelegate::applicationWillEnterForeground()
{
Director::getInstance()->startAnimation();
SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
}
我们在AppDelegate类当中可以找到执行我们Lua脚本的方法,下面来看一下main.lua这个文件,我们屏幕显示的逻辑实现全部在这个文件中可以看到:
>>>main.lua
require “Cocos2d”
require “Cocos2dConstants”
– cclog
cclog = function(…)
print(string.format(…))
end
– 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”)
– avoid memory leak 这是脚本回收参数,避免内存泄漏
collectgarbage(“setpause”, 100)
collectgarbage(“setstepmul”, 5000)
– 追加资源的搜索顺序
cc.FileUtils:getInstance():addSearchResolutionsOrder(“src”);
cc.FileUtils:getInstance():addSearchResolutionsOrder(“res”);
local schedulerID = 0
–support debug 获取目标平台
local targetPlatform = cc.Application:getInstance():getTargetPlatform()
if (cc.PLATFORM_OS_IPHONE == targetPlatform) or (cc.PLATFORM_OS_IPAD == targetPlatform) or
(cc.PLATFORM_OS_ANDROID == targetPlatform) or (cc.PLATFORM_OS_WINDOWS == targetPlatform) or
(cc.PLATFORM_OS_MAC == targetPlatform) then
cclog("result is ")
–require(‘debugger’)()
end
– 类似c++的include,引入文件,会检查是否重复引入
require “hello2”
– 调用外部函数,在hello2.lua中
cclog("result is " … myadd(1, 1))
– 获取可视区域
local visibleSize = cc.Director:getInstance():getVisibleSize()
– 可视原点坐标 OpenGL坐标系,左下角为原点
local origin = cc.Director:getInstance():getVisibleOrigin()
– add the moving dog 添加移动的小松鼠
local function creatDog()
– 每一帧尺寸设置,local表示局部变量
local frameWidth = 105
local frameHeight = 95
– create dog animate 加载动画资源并创建精灵帧
– 加载精灵动画所在纹理
local textureDog = cc.Director:getInstance():getTextureCach