cocos2dx-lua项目的构建、编译细则

来源:互联网 发布:mac 磁盘格式 编辑:程序博客网 时间:2024/04/29 05:59


首先说明你下本文使用
cocos2dx version 2.1.4
lua version 5.1
第三方摇杆 SneakyButton.cpp \
           SneakyButtonSkinnedBase.cpp \
           SneakyJoystick.cpp \
           SneakyJoystickSkinnedBase.cpp \


1, create-android-project.bat 生成相应工程,下文简称Test工程
2, VS新建支持LUA的cocos2dx项目
3, 把VS工程的proj.win32, Classes, Resources拷贝到 Test工程目录内(当然也可以不拷贝,我是觉得这样方便,不用在以后完成的时候再去VS里面拷 CPP,推荐拷贝)
4, 把cocos2dx里面的tolua++也拷到 Test工程目录 (因为有自定义类,没有可以不用。 lua绑定cpp的-->

http://blog.csdn.net/musicvs/article/details/8166572

http://blog.csdn.net/musicvs/article/details/8166655

)
5, Resources里面增加了一个script文件夹,lua文件都放到此目录, 编译出现“get data from file failed、Cocos2dxActivity cannot be ”路径等问题


看下AppDelegate.cpp代码
....
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) // android平台
    CCString* pstrFileContent = CCString::createWithContentsOfFile("hello.lua");
    if (pstrFileContent)
    {
        pEngine->executeString(pstrFileContent->getCString());
    }
#else // WIN32 平台
    std::string path = CCFileUtils::sharedFileUtils()->fullPathForFilename("hello.lua");
    pEngine->addSearchPath(path.substr(0, path.find_last_of("/")).c_str()); //查找路径设置,其它lua文件也会按照这个路径查找
    pEngine->executeScriptFile(path.c_str());
#endif
.....


应该改成 
....
std::string dirPath = "script";
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) // android平台, 路径分隔应该同linux 用"/"
    CCString* pstrFileContent = CCString::createWithContentsOfFile( (dirPath + "/hello.lua").c_str() );
    if (pstrFileContent)
    {
        pEngine->executeString(pstrFileContent->getCString());
    }
#else // WIN32 平台
    std::string path = CCFileUtils::sharedFileUtils()->fullPathForFilename( (dirPath + "\\hello.lua").c_str() );
    pEngine->addSearchPath(path.substr(0, path.find_last_of("\\")).c_str()); 
    //path.substr(0, path.find_last_of("\\")) 查找最后一个"\\"字符位置
    pEngine->executeScriptFile(path.c_str());
#endif
.....


好,进行编译,没错!?那就运行吧...出现黑屏,VS的输出
"Get data from file(hello2.lua) failed!
can not get file data of hello2.lua"

还是路径...


打开 hello.lua
....
    require "hello2"
    cclog("result is " .. myadd(3, 5))

....
改成
....
    require "script/hello2"
    cclog("result is " .. myadd(3, 5))

....
再运行看看。。
欧耶.。效果出来了
--------------------------------
好, 我再在script文件夹里面增加一个aa文件夹,aa文件夹里面放hello3.lua,内容如下
function myadd2(x, y)
    return x + y
end


hello.lua添加下面两句
    require "script/aa/hello3"
    cclog("result2 is " .. myadd2(2,4))



再次运行...没错。。


6, 修改Android.mk
LOCAL_PATH := $(call my-dir)


include $(CLEAR_VARS)


LOCAL_MODULE := game_shared


LOCAL_MODULE_FILENAME := libgame




LOCAL_SRC_FILES := hellocpp/main.cpp \
                   ../../Classes/AppDelegate.cpp \
                   ../../Classes/SneakyButton.cpp \
                   ../../Classes/SneakyButtonSkinnedBase.cpp \
                   ../../Classes/SneakyJoystick.cpp \
                   ../../Classes/SneakyJoystickSkinnedBase.cpp \
                   ../../Classes/cocos2dx_support/LuaCocos2d.cpp 
                   #这里如果有自定义的CPP,如本文增加自定义SneakyButton.cpp,SneakyButtonSkinnedBase.cpp, SneakyJoystick.cpp, SneakyJoystickSkinnedBase.cpp文件,tolua++处理后生成LuaCocos2d.cpp,文件包含上面四个文件的接口就应该修改 X:\cocos2d_Home\script\lua\proj.android\Android.mk,把该文件中包含的LuaCocos2d.cpp注释。。同理若lua工程的mk跟当前的mk不能有重复的
                   
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes \
                    $(LOCAL_PATH)/../../Classes/cocos2dx_support


LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static \
                                cocosdenshion_static \
                                box2d_static \
                                cocos_lua_static
            
include $(BUILD_SHARED_LIBRARY)


$(call import-module,CocosDenshion/android) \
$(call import-module,cocos2dx) \
#$(call import-module,extensions) \  这里注释掉因为lua的mk已经加入
$(call import-module,external/Box2D) \
$(call import-module,scripting/lua/proj.android)  
#这里链接scripting/lua/proj.android的mk文件


7,修改build_native.sh
主要设置几个环境变量


......
# paths
NDK_ROOT="/cygdrive/d/android-ndk-r8e" #####加入NDK_ROOT, cygwin路径是用/cygdrive/开头
if [ -z "${NDK_ROOT+aaa}" ];then
echo "please define NDK_ROOT"
exit 1
fi


DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# ... use paths relative to current directory


COCOS2DX_ROOT="/cygdrive/e/cocos2dx/cocos2d-x-2.1.4"  #####cocos2dx的目录
APP_ROOT="/cygdrive/e/cocos2dx/cocos2d-x-2.1.4/Test"  #####app的目录
APP_ANDROID_ROOT="/cygdrive/e/cocos2dx/cocos2d-x-2.1.4/Test/proj.android"#####app的android目录


echo "NDK_ROOT = $NDK_ROOT"
echo "COCOS2DX_ROOT = $COCOS2DX_ROOT"
echo "APP_ROOT = $APP_ROOT"
........


最后用cygwin执行此文件。。。没错就生成so了