HelloCpp与HelloLua的区别
HelloCpp工程的AppDelegate
bool AppDelegate::applicationDidFinishLaunching() { ..... // create a scene. it's an autorelease object CCScene *pScene = HelloWorld::scene(); // run pDirector->runWithScene(pScene); return true; }
改为
HelloLua工程的AppDelegate
bool AppDelegate::applicationDidFinishLaunching() { ..... // register lua engine 注册lua引擎 CCLuaEngine* pEngine = CCLuaEngine::defaultEngine(); // 1. 通过LuaEngine的静态函数获取一个Lua脚本引擎的实例对象指针 CCScriptEngineManager::sharedManager()->setScriptEngine(pEngine); // 2. 获取单例脚本引擎的实例对象指针 然后将Lua脚本引擎的实例对象指针设为脚本引擎管理器当前进行管理的脚本引擎 #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) // 如果是安卓平台,编译这个 CCString* pstrFileContent = CCString::createWithContentsOfFile("Myhello.lua"); if (pstrFileContent) { pEngine->executeString(pstrFileContent->getCString()); } #else std::string path = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("Myhello.lua"); // 获取文件hello.lua的全部路径,因为该文件在资源文件夹内,直接写名字就行 pEngine->addSearchPath(path.substr(0, path.find_last_of("/")).c_str()); // 将这个路径的目录放入到脚本引擎的搜索目录 pEngine->executeScriptFile(path.c_str()); // 3. 执行这个路径所指向的Lua文件 #endif return true; }
然后在lua文件里创建场景与运行
-- run local sceneGame = CCScene:create() sceneGame:addChild(createLayerFarm()) sceneGame:addChild(createLayerMenu()) CCDirector:sharedDirector():runWithScene(sceneGame)
HelloLua的AppDelegate源文件与TestLua的区别
HelloLua工程
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) // 如果是安卓平台,编译这个 CCString* pstrFileContent = CCString::createWithContentsOfFile("Myhello.lua"); if (pstrFileContent) { pEngine->executeString(pstrFileContent->getCString()); } #else std::string path = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(<strong>"Myhello.lua"</strong>); // 获取文件hello.lua的全部路径,因为该文件在资源文件夹内,直接写名字就行 pEngine->addSearchPath(path.substr(0, <strong>path.find_last_of("/")</strong>).c_str()); // 将这个路径的目录放入到脚本引擎的搜索目录 pEngine->executeScriptFile(path.c_str()); // 3. 执行这个路径所指向的Lua文件 #endif
TestLua工程
std::string <strong>dirPath = "luaScript"</strong>; #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) CCString* pstrFileContent = CCString::createWithContentsOfFile((dirPath + "/controller.lua").c_str()); if (pstrFileContent) { pEngine->executeString(pstrFileContent->getCString()); } #else std::string path = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(<strong>(dirPath + "/controller.lua").c_str()</strong>); pEngine->addSearchPath(path.substr(0, <strong>path.find_last_of("/") - dirPath.length()</strong>).c_str()); pEngine->executeScriptFile(path.c_str()); #endif