2012年1月29日 星期日

在cocos2d-X中可以支援lua的使用

cocos2d-X中可以支援lua的使用(支援程度未確認,不過應該會越來越高)
使用方式重點應該在怎麼載入執行lua檔吧。

所以就簡單的記一下方式.
appdelegate.cpp中的
bool AppDelegate::applicationDidFinishLaunching()
return true之前加上

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
        unsigned long size;
        char *pFileContent = (char*)CCFileUtils::getFileData("hello.lua", "r", &size);

        if (pFileContent)
        {
            // copy the file contents and add '\0' at the end, or the lua parser can not parse it
            char *pCodes = new char[size + 1];
            pCodes[size] = '\0';
            memcpy(pCodes, pFileContent, size);
            delete[] pFileContent;

            CCScriptEngineManager::sharedScriptEngineManager()->getScriptEngine()->executeString(pCodes);
            delete []pCodes;
        }
#endif

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
        string path = CCFileUtils::fullPathFromRelativePath("hello.lua");
        CCScriptEngineManager::sharedScriptEngineManager()->getScriptEngine()->addSearchPath(path.substr(0, path.find_last_of("/")).c_str());
    CCScriptEngineManager::sharedScriptEngineManager()->getScriptEngine()->executeScriptFile(path.c_str());
#endif

即可載入執行 hello.lua
以這邊的寫法來看,android ios的方式不一樣

其實在cocos2d-x的目錄下,就有一個HelloLua的目錄,
從目錄下的ios子目錄就可以開啟ios的專案來看示範

沒有留言:

張貼留言