做开发就要快速建模,而学习一个库就要快速将demo跑起来。
之前百度地图API也是一样,跑起来,看看它的运行状况是什么样的,有了感性认识之后,就更容易理解了。
Cocos2d-x是一个开源的移动游戏框架,源自cocos2d。其跨平台性导致它的包里有太多东西,而我呢,只想Android部分的。对此,我很是纠结。
因为看官网的文档,看的我是云里雾里的,总是找不到从何入手。上面描述的环境搭建复杂至极,真是令人抓狂。
今天看了一篇文章,让我硬着头皮又操练起来,终于成了。
文章名为:android基于cocos2d-x的eclipse开发环境搭建
我的开发环境在Linux下,NDK就不用那么麻烦了,Android应用开发环境也是之前就搭建好的。只等着把Cocos2d-x加进来了。
SimpleGame是其提供的一个sample,路径在cocos2d-x-2.2.1/cocos2d-x-2.2.1/samples/Cpp,下面是各个平台的式例。(题外话:之前我直接找sample,一看里面没有android目录,我就很失望的走开了。)
根据上面的文章,很快的,demo就跑起来了。
具体步骤如下:
1.将libcocos2dx导入到eclipse
libcocos2dx是android专用的cocos2dx java库,很多有用的类都在此库中,比如例子中要用到的Cocos2dxActivity。
路径在cocos2d-x-2.2.1/cocos2dx/platform/android/java
2.将samples/Cpp/SimpleGame/proj.android 导入到eclipse
3.复制游戏需要的资源
cp cocos2d-x-2.2.1/samples/Cpp/SimpleGame/Resources/* cocos2d-x-2.2.1/samples/Cpp/SimpleGame/proj.android/assets/
4.复制cocos2d-x-2.2.1目录下的cocos2dx,CocosDenshion,extensions,external这4个目录到你的NDK的sources目录下,以我的电脑为例,就是/android/android-ndk-r9b/sources
5.最讨厌的,把cocos2dx/platform/third_party/android/prebuilt下的libcurl,libjpeg,libpng,libtiff,libwebp再拷到/android-ndk-r9b/sources下
5.设置NDK Location
Eclipse中Window--->Preferences--->Android--->NDK,在NDK Location中填上或找到你ndk所在的目录,比如我的:/home/linc/dev/android/android-ndk-r9b
6.设置ndk-build的命令,为 ndk-build NDK_DEBUG=1
点击项目SimapleGame,Alt+Enter,进入Properties for SimapleGame,点击C/C++ Build,在Build command中添加ndk-build NDK_DEBUG=1,点击Apply。
7.接上面,点开C/C++ Build,点击Tool Chain Editor,将Current builder选成Android Builder,OK。
8.项目SimapleGame右键Build Configurations--->Build all,或者单独此项目时,Project--->Build all。
在Eclipse的Console中打印出编译信息,从这些信息中我们可以看到到底有多少个文件参与了此次编译。
**** Build of configuration Default for project SimpleGame **** /home/linc/dev/android/android-ndk-r9b/ndk-build all Android NDK: WARNING:/home/linc/dev/android/android-ndk-r9b/sources/cocos2dx/Android.mk:cocos2dx_static: LOCAL_LDLIBS is always ignored for static libraries [armeabi] Compile++ thumb: game_shared <= main.cpp [armeabi] Compile++ thumb: game_shared <= AppDelegate.cpp [armeabi] Compile++ thumb: game_shared <= HelloWorldScene.cpp [armeabi] Compile++ thumb: game_shared <= GameOverScene.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCConfiguration.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCScheduler.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCCamera.cpp [armeabi] Compile thumb : cocos2dx_static <= ccFPSImages.c [armeabi] Compile++ thumb: cocos2dx_static <= CCAction.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCActionCamera.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCActionCatmullRom.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCActionEase.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCActionGrid.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCActionGrid3D.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCActionInstant.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCActionInterval.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCActionManager.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCActionPageTurn3D.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCActionProgressTimer.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCActionTiledGrid.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCActionTween.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCAtlasNode.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCNode.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCAffineTransform.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCGeometry.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCAutoreleasePool.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCDictionary.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCNS.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCObject.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCSet.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCString.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCZone.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCArray.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCDataVisitor.cpp [armeabi] Compile++ thumb: cocos2dx_static <= cocos2d.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCDirector.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCDrawingPrimitives.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCDrawNode.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCGrabber.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCGrid.cpp [armeabi] Compile thumb : cocos2dx_static <= aabb.c [armeabi] Compile thumb : cocos2dx_static <= mat3.c [armeabi] Compile thumb : cocos2dx_static <= mat4.c [armeabi] Compile thumb : cocos2dx_static <= neon_matrix_impl.c [armeabi] Compile thumb : cocos2dx_static <= plane.c [armeabi] Compile thumb : cocos2dx_static <= quaternion.c /home/linc/dev/android/android-ndk-r9b/sources/cocos2dx/kazmath/src/quaternion.c: In function 'kmQuaternionAssign': /home/linc/dev/android/android-ndk-r9b/sources/cocos2dx/kazmath/src/quaternion.c:478:5: warning: incompatible implicit declaration of built-in function 'memcpy' [enabled by default] [armeabi] Compile thumb : cocos2dx_static <= ray2.c [armeabi] Compile thumb : cocos2dx_static <= utility.c [armeabi] Compile thumb : cocos2dx_static <= vec2.c [armeabi] Compile thumb : cocos2dx_static <= vec3.c [armeabi] Compile thumb : cocos2dx_static <= vec4.c /home/linc/dev/android/android-ndk-r9b/sources/cocos2dx/kazmath/src/vec4.c: In function 'kmVec4Assign': /home/linc/dev/android/android-ndk-r9b/sources/cocos2dx/kazmath/src/vec4.c:150:5: warning: incompatible implicit declaration of built-in function 'memcpy' [enabled by default] [armeabi] Compile thumb : cocos2dx_static <= mat4stack.c [armeabi] Compile thumb : cocos2dx_static <= matrix.c [armeabi] Compile++ thumb: cocos2dx_static <= CCKeypadDelegate.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCKeypadDispatcher.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCLabelAtlas.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCLabelBMFont.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCLabelTTF.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCLayer.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCScene.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCTransitionPageTurn.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCTransition.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCTransitionProgress.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCMenu.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCMenuItem.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCClippingNode.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCMotionStreak.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCProgressTimer.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCRenderTexture.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCParticleExamples.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCParticleSystem.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCParticleBatchNode.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCParticleSystemQuad.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCImageCommonWebp.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCSAXParser.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCThread.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCFileUtils.cpp [armeabi] Compile++ thumb: cocos2dx_static <= platform.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCEGLViewProtocol.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCDevice.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCEGLView.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCAccelerometer.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCApplication.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCCommon.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCFileUtilsAndroid.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCImage.cpp [armeabi] Compile++ thumb: cocos2dx_static <= Java_org_cocos2dx_lib_Cocos2dxBitmap.cpp [armeabi] Compile++ thumb: cocos2dx_static <= Java_org_cocos2dx_lib_Cocos2dxHelper.cpp [armeabi] Compile++ thumb: cocos2dx_static <= Java_org_cocos2dx_lib_Cocos2dxRenderer.cpp [armeabi] Compile++ thumb: cocos2dx_static <= Java_org_cocos2dx_lib_Cocos2dxAccelerometer.cpp [armeabi] Compile++ thumb: cocos2dx_static <= JniHelper.cpp [armeabi] Compile++ thumb: cocos2dx_static <= IMEJni.cpp [armeabi] Compile++ thumb: cocos2dx_static <= TouchesJni.cpp [armeabi] Compile++ thumb: cocos2dx_static <= DPIJni.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCScriptSupport.cpp [armeabi] Compile++ thumb: cocos2dx_static <= ccShaders.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCGLProgram.cpp [armeabi] Compile++ thumb: cocos2dx_static <= ccGLStateCache.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCShaderCache.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCAnimation.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCAnimationCache.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCSprite.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCSpriteBatchNode.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCSpriteFrame.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCSpriteFrameCache.cpp [armeabi] Compile++ thumb: cocos2dx_static <= ccUTF8.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCNotificationCenter.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCProfiling.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCPointExtension.cpp [armeabi] Compile++ thumb: cocos2dx_static <= TransformUtils.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCUserDefaultAndroid.cpp [armeabi] Compile++ thumb: cocos2dx_static <= base64.cpp [armeabi] Compile++ thumb: cocos2dx_static <= ccUtils.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCVertex.cpp [armeabi] Compile++ thumb: cocos2dx_static <= ccCArray.cpp [armeabi] Compile++ thumb: cocos2dx_static <= TGAlib.cpp [armeabi] Compile++ thumb: cocos2dx_static <= tinyxml2.cpp [armeabi] Compile++ thumb: cocos2dx_static <= ZipUtils.cpp [armeabi] Compile++ thumb: cocos2dx_static <= ioapi.cpp [armeabi] Compile++ thumb: cocos2dx_static <= unzip.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCComponent.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCComponentContainer.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCIMEDispatcher.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCTextFieldTTF.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCTexture2D.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCTextureAtlas.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCTextureCache.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCTextureETC.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCTexturePVR.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCParallaxNode.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCTMXLayer.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCTMXObjectGroup.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCTMXTiledMap.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCTMXXMLParser.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCTileMapAtlas.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCTouchDispatcher.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCTouchHandler.cpp [armeabi] Compile++ thumb: cocos2dx_static <= CCTouch.cpp [armeabi] StaticLibrary : libcocos2d.a [armeabi] Compile++ thumb: cocosdenshion_static <= SimpleAudioEngine.cpp [armeabi] Compile++ thumb: cocosdenshion_static <= SimpleAudioEngineJni.cpp [armeabi] Compile++ thumb: cocosdenshion_static <= OpenSLEngine.cpp [armeabi] Compile++ thumb: cocosdenshion_static <= SimpleAudioEngineOpenSL.cpp [armeabi] StaticLibrary : libcocosdenshion.a [armeabi] Compile thumb : cpufeatures <= cpu-features.c [armeabi] StaticLibrary : libcpufeatures.a [armeabi] SharedLibrary : libgame.so [armeabi] Install : libgame.so => libs/armeabi/libgame.so **** Build Finished ****
下面是我遇到的错误:
错误1. non-numeric second argument to `wordlist' function
此问题是NDK开发中常遇到的问题,原因是AndroidManifest.xml没有指定SDK版本。
解决办法是添加:
<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="10"/>错误2.创建的Android项目提示org.cocos2dx.lib.Cocos2dxActivity
可能是java那个项目没有导入进来吧,报错是找不到这个类。直接搞过来吧,将org.cocos2dx.lib包下的类全拷进来就可以了。
这是因为第一步没有做好,java库没有导入进来的缘故。