现在的位置: 首页 > 综合 > 正文

cocos2dx开发-让例子(新建cocos项目)在android机器上跑起来

2018年05月03日 ⁄ 综合 ⁄ 共 4245字 ⁄ 字号 评论关闭

新建Cocos项目

解压源代码得到文件夹cocos2d-x-3.2alpha0

进入目录cocos2d-x-3.2alpha0\tools\cocos2d-console\bin执行

python cocos.py new BuckGameCC -p com.cocos2dx.BuckGameCC -l cpp -d ..\..\..\

这样在cocos2d-x-3.2alpha0目录下得到文件夹BuckGameCC

新建quickcocos项目

下载并解压得到目录quick-cocos2dx-x-2.2.4-release目录

这个版本player.exe新建项目功能不能用,用命令行创建空项目:bin目录中运行命令

create_project -p com.quick2dx.buckgame -o ../buckgame

得到文件夹quick-cocos2dx-x-2.2.4-release\buckgame,以后所有的任务就是修改这个目录中的scripts目录下的lua文件,根据需要也可能需要修改sources中的c++

生成项目vs2012调试需要修改调试目录,否则会找不到lua,$(ProjectDir)改为$(ProjectDir)..

碰到问题与解决方案

工作需要学习移动开发了,换个平台真是考验,让例子在android上跑起来也不容易

直接下载adt-bundle-windows-x86-20140321,照着android官方教程用java开发程序是没问题的,但是想把cocos2dx的c++例子跑起来费了不少周折。

照着cocos2dx中文文档一步一步做,如果完全照做应该问题也不大,先说说因为某些步骤没有做好出现的问题:

1. ndk版本不对,32位系统与64位系统一定要确定没下错

2. 解压后的目录D:\android\android-ndk-r9d里面文件大小都是0,显然出问题了

如果编译build_native.py的时候有问题,一般都是ndk出问题了。

3. androidSDK版本一定要有,下载的如果是几百M的话通常只有最新版本sdk,如果是5G的应该比较全,也有可能不够新,可以用sdkmanager查看。

最后导入到eclipse以后,有错误,需要把cocos\platform\android\java\src里面的org目录复制到当前项目的src目录下。

英文文档里面的问题解决方案很好,如果出问题可以确定下这3步有没有问题:

If you get errors in the Eclipse when you imported the Android project. Don't worry, just import the libcocos2dx project will get ride of it.
If you imported all the projects and the errors remains, you might need to build the libcocos2dx project manually and to see whether there is a libcocos2dx.jar file under the bin directory.
If you installed the project in your Android phone but it crashed when launching. You should make sure there is a libxxx.so file under the libs/armeabi directory.。

但是即使完全按照文档做并且不出问题,也很难一帆风顺的跑起来

比如,import libcocos2dx以后,还是提示找不到Cocos2dxActivity,因为依赖关系有问题,解决方法:项目右键-property-android-library,把没用的删掉,把libcocos2dx import进来。

两种运行android设备程序方法

环境搭好以后,对于cocos2d-x-3.2alpha0,有2种方法可以跑起来,其实原理一样:

编译运行例子程序:

1.在cocos2d-x-3.2alpha0/目录中执行python setup.py,确保环境变量没问题

2.在cocos2d-x-3.2alpha0/build目录中python android-build.py -p 10 cpp-tests,cpp-tests也可以是cpp-empty-test等例子程序

3.在cocos2d-x-3.2alpha0/tests/cpp-tests/proj.android/bin中执行adb install CppTests-debug.apk 

从例子程序中复制:

1.在cocos2d-x-3.2alpha0/tools/cocos2d-console/bin目录执行python cocos.py new test -p com.coco2dx.org -l cpp -d ./testfolder,新建一个叫做test的项目,位于当前目录的testfolder中。

2.在cocos2d-x-3.2alpha0/tools/cocos2d-console/bin/testfolder/MyCppGame\proj.android中运行python build_native.py,这样只是编译,因为没有设置-p参数,还需要将项目导入到eclipse才可以生成bin目录,如果希望直接编译生成.apk文件,只需加参数-p 10即可。

python文件分析调试

对于build_native.py,可以通过print命令输出信息进行调试,也可以使用python自带的IDLE进行调试,调试过程中可能会提示错误,因为python要求不能有tab,需要用空格表示缩进,按照提示操作即可:全选-Format-Untablity Region。还可能提示__file__不能识别,可以将os.path.dirname(os.path.realpath(__file__))替换为os.getcwd()。

IDLE的调试方法是,Run-Python Shell,在打开的Shell中Debug-Debugger,然后再IDLE中Run-Run Module(F5),然后就可以在刚才打开的Debugger中进行调试。

容易发现,build_native.py执行的命令是D:\android\android-ndk-r9d\ndk-build -j4 -C D:\android\cocos2d\cocos2d-x-3.2alpha0\tools\cocos2d-console\bin\test\MyCppGame\proj.android NDK_DEBUG=1

而对于python android-build.py -p 10 cpp-tests,IDLE中调试需要添加sys.argv=[sys.argv[0],'-p','10','cpp-tests'],作为参数,调试完以后注释掉。执行的命令以此是

D:\android\android-ndk-r9d\ndk-build -j4 -C D:\android\cocos2d\cocos2d-x-3.2alpha0\build\..\tests/cpp-tests/proj.android NDK_DEBUG=1 NDK_MODULE_PATH=D:\android\cocos2d\cocos2d-x-3.2alpha0\;D:\android\cocos2d\cocos2d-x-3.2alpha0\external;D:\android\cocos2d\cocos2d-x-3.2alpha0\cocos

D:\android\adt-bundle-windows-x86-20140321\sdk\tools/android update lib-project -t android-10 -p D:\android\cocos2d\cocos2d-x-3.2alpha0\cocos/platform/android/java

D:\android\adt-bundle-windows-x86-20140321\sdk\tools/android update project -t android-10 -p D:\android\cocos2d\cocos2d-x-3.2alpha0\tests/cpp-tests/proj.android -s

ant clean debug -f D:\android\cocos2d\cocos2d-x-3.2alpha0\tests/cpp-tests/proj.android\build.xml -Dsdk.dir=D:\android\adt-bundle-windows-x86-20140321\sdk

c++编译

ndk编译相关可以从ndk文档overview.html开始看。
ndk-build.cmd执行命令"%NDK_ROOT%prebuilt/%NDK_WIN_HOST%/bin/make.exe" -f "%NDK_ROOT%build/core/build-local.mk" SHELL=cmd %*
对于windows系统,为"%NDK_ROOT%prebuilt/windows/bin/make.exe" -f "%NDK_ROOT%build/core/build-local.mk" SHELL=cmd %*
即make:build-local.mk

ndk-build以后会增加3个目录:assets、libs、obj,

ant打包

ant clean debug -f D:\android\cocos2d\cocos2d-x-3.2alpha0\tests/cpp-tests/proj.android\build.xml -Dsdk.dir=D:\android\adt-bundle-windows-x86-20140321\sdk

build.xml文件里面设置projectname,命令行中设置了sdk.dir,最后调用${sdk.dir}/tools/ant/build.xml。
build.xml可以用eclipse打开:Window-Show view-ant,在ant界面点击Add buildfiles添加要查看的build.xml
具体build过程网上有,比如http://www.oschina.net/question/54100_30501

抱歉!评论已关闭.