在网上找到一个用cocos2d-x做的仿flappybird的游戏:
https://github.com/OiteBoys/Earlybird
下载了3.1版本的源代码然后打算自己打个包到Android上。解决完代码上的几个小问题(如3.1版本没有调用设置搜索路径的函数)后,打包的过程中出现下面错误,下面是问题描述和解决过程。
1. fatal error: CCMenuItem.h: No such file or directory
这是因为工程中AppDelagete.cpp包含了这个头文件,但是在头文件搜寻路径中没有找到该文件。
在vs或其他ide上一般只要设置工程属性就行了。比如vs2012:
工程属性面板->配置属性->C/C++->附加包含目录,加上cocos2d-x/cocos/2d就行了。
设置了ide的工程属性后,对于命令行编译打包到android平台apk仍然会提示这个问题。这时候就需要在Android.mk里面设置,让编译程序能够找到对应的头文件。比如这种情况就可以这样设置:
在Andoroid.mk (proj.android\jni\下面)里面把头文件加入LOCAL_C_INCLUDES,比如将
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../cocos2d/cocos/2d \
$(LOCAL_PATH)/../../Classes
这里我原以为是要在Android.mk里面加入一行$(call import-module,2d)。结果这样做导致提示2d这个模块找不到,进而错误地认为是ndk路径设置不对:
Android NDK: F:\Tech\Projects\Cocos2d-x\Earlybird\Earlybird-3.1\proj.android\../
cocos2d/cocos/audio/android/Android.mk: Cannot find module with tag '2d' in impo
rt path
Android NDK: Are you sure your NDK_MODULE_PATH variable is properly defined ?
Android NDK: The following directories were searched:
Android NDK:
make.exe: Entering directory `F:/Tech/Projects/Cocos2d-x/Earlybird/Earlybird-3.1
/proj.android'
jni/Android.mk:35: *** Android NDK: Aborting. . Stop.
后来才发现应该是LOCAL_C_INCLUDES里面添加。
2. 如下错误信息:
[armeabi] SharedLibrary : libcocos2dcpp.so
jni/../../Classes/LoadingScene.h:30: error: undefined reference to 'LoadingScene
::LoadingScene()'
jni/../../Classes/HelloWorldScene.cpp:53: error: undefined reference to 'AtlasLo
ader::getInstance()'
很明显,这里没有找到对应的LoadingScene.cpp和AtlasLoader.cpp文件。这两个文件都在../../Classes文件夹里面。但是在Android.mk里面../../Classes只是头文件的搜索路径,没有放到编译的文件里面。所以这里在链接的时候找不到对应的源文件。应该在LOCAL_SRC_FILES里面加入,将
LOCAL_SRC_FILES := hellocpp/main.cpp \
../../Classes/AppDelegate.cpp \
../../Classes/HelloWorldScene.cpp
改成
LOCAL_SRC_FILES := hellocpp/main.cpp \ ../../Classes/AppDelegate.cpp \ ../../Classes/HelloWorldScene.cpp \ ../../Classes/AtlasLoader.cpp \ ../../Classes/BackgroundLayer.cpp \ ../../Classes/BirdSprite.cpp \ ../../Classes/GameLayer.cpp \ ../../Classes/GameScene.cpp \ ../../Classes/LoadingScene.cpp \ ../../Classes/MenuScene.cpp \ ../../Classes/Number.cpp \ ../../Classes/OptionLayer.cpp \ ../../Classes/StatusLayer.cpp \ ../../Classes/WelcomeLayer.cpp \ ../../Classes/WelcomeScene.cpp \ ../../Classes/UserRecord.cpp \