//1、
//cocos 程序开始运行时执行的函数
bool AppDelegate::applicationDidFinishLaunching()
{
// initialize director
auto director = Director::getInstance();
director->setProjection(Director::Projection::_2D);
// turn on display FPS
director->setDisplayStats(true);
// set FPS. the default value is 1.0/60 if you don't call this
director->setAnimationInterval(1.0 / 60);
director->startAnimation();
// register lua engine
auto engine = LuaEngine::getInstance();
ScriptEngineManager::getInstance()->setScriptEngine(engine);
lua_State* L = engine->getLuaStack()->getLuaState();
lua_module_register(L);
// If you want to use Quick-Cocos2dx-Community, please uncomment below code
quick_module_register(L);
LuaStack* stack = engine->getLuaStack();
// stack->setXXTEAKeyAndSign("2dxLua", "XXTEA");
//主要来看下这部分:
StartupCall *call = StartupCall::create(this);
call->startup();
return true;
}
//2、
void StartupCall::startup()
{
auto engine = LuaEngine::getInstance();
auto stack = engine->getLuaStack();
const ProjectConfig &project = _app->_project;
// set search path
// 设置工程搜索路径
string path = FileUtils::getInstance()->fullPathForFilename(project.getScriptFileRealPath().c_str());
size_t pos;
while ((pos = path.find_first_of("\\")) != std::string::npos)
{
path.replace(pos, 1, "/");
}
size_t p = path.find_last_of("/");
string workdir;
if (p != path.npos)
{
workdir = path.substr(0, p);
// Lua 代码文件的搜索需要包含src
stack->addSearchPath(workdir.c_str());
// cc.FileUtils的文件搜索去掉src目录
size_t p = workdir.find_last_of("/");
workdir = path.substr(0, p);
FileUtils::getInstance()->addSearchPath(workdir);
}
// load framework
if (project.isLoadPrecompiledFramework())
{
const string precompiledFrameworkPath = project.getPrecompiledFrameworkPath();
stack->loadChunksFromZIP(precompiledFrameworkPath.c_str());
}
// load script
string env = "__LUA_STARTUP_FILE__=\"";
env.append(path);
env.append("\"");
engine->executeString(env.c_str());
CCLOG("------------------------------------------------");
CCLOG("LOAD LUA FILE: %s", path.c_str());
CCLOG("------------------------------------------------");
engine->executeScriptFile(path.c_str());
// track start event
trackLaunchEvent();
}
//跟踪启动事件
void StartupCall::trackEvent(const char *eventName)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
const char *platform = "win";
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
const char *platform = "mac";
#else
const char *platform = "UNKNOWN";
#endif
HTTPRequest *request = HTTPRequest::createWithUrl(NULL,
"http://www.google-analytics.com/collect",
kCCHTTPRequestMethodPOST);
request->addPOSTValue("v", "1");
request->addPOSTValue("tid", "UA-84326395-1");
request->addPOSTValue("cid", Native::getOpenUDID().c_str());
request->addPOSTValue("t", "event");
request->addPOSTValue("an", "player");
request->addPOSTValue("av", cocos2dVersion());
request->addPOSTValue("ec", platform);
request->addPOSTValue("ea", eventName);
request->start();
}