Cocos2d-x 3.1 environment construction and project creation

Cocos2d-x 3.x has been revised a lot. I used the environment once before, but I didn't take screenshots. This time, I took advantage of the reinstallation of the computer and took screenshots while building. This blog post is just for documentation and not for teaching, so it's more about the build process. This article basically refers to this blog: http://www.idcfree.com/article-1488-1.html , this blog is more detailed than me, but mine is a rookie tutorial, you can refer to it if you like to see pictures my blog.


Installation tool:

1. Download JDK

New environment variable: JAVA_HOME value: E:\Program Files (x86)\Java\jdk1.7.0_02

New environment variable: The value of CLASSPATH is: .;%JAVA_HOME%\lib; (Note: The dot indicates the current directory, which cannot be omitted)

Add the following content in front of the value of the system variable Path: %JAVA_HOME%\bin; (Note: the semicolon here cannot be omitted) Here, the JDK installation is complete.

Click "Start" -> "Run" -> enter: cmd command, enter in the CMD window: java -version 
The following interface appears, indicating that the installation is successful:


2. Download the Android SDK

  • Download address: http://developer.android.com/sdk/index.html
  • Android development tools ADT (Android Developer Tools), including Eclipse and SDK, go to the official website to download, or use the green version, I am using the green version, after downloading, unzip F:\Android :



3. Download Android NDK


4. Download ANT

  • Download address: http://ant.apache.org/bindownload.cgi
  • ANT is used to automatically build Android programs
  • After downloading, extract it to the F:\Android directory. So far, the F:\Android directory has the following contents

  


5. Download python

  • Download address: https://www.python.org/ftp/python/2.7.3/python-2.7.3.msi
  • The original cocos2d-x uses different scripts to create projects on different platforms, but after 3.0, python is used uniformly
  • Install python, my installation path is: C:\Program Files (x86)\Python27
  • Configure python environment variables

  Add the installation path C:\Program Files (x86)\Python27 to the path environment variable

  Click "Start" -> "Run" -> Enter: cmd command, enter in the CMD window: python 
  The following interface appears, indicating that the installation is successful:

  



Cocos2d-x download and configuration

1. Download the Cocos2d-x engine

  • 下载地址:http://cn.cocos2d-x.org/
  • 上官网下载最新引擎后解压,我用的是cocos2d-x-3.1.1,解压目录是F:\Cocos2d-x


2、设置环境变量

需要配置下面四个环境变量:

  • COCOS_CONSOLE_ROOT: cocos控制台路径,用于新建、构建和发行工程。
  • NDK_ROOT:NDK根目录
  • ANDROID_SDK_ROOT:  SDK根目录
  • ANT_ROOT:  ANT根目录

这些变量可以通过引擎根目录的setup.py来配置,打开引擎根目录F:\Cocos2d-x,运行setup.py,一步步输入下面路径:

->COCOS_CONSOLE_ROOT : F:\Cocos2d-x\tools\cocos2d-console\bin
->NDK_ROOT : F:\Android\android-ndk-r9b
->ANDROID_SDK_ROOT : F:\Android\sdk
->ANT_ROOT : F:\Android\ant\bin


配置好之后就可以在环境变量看到添加了这些东东:



测试以下环境变量是否生效,打开cmd,如果出现下面这种情况,则配置成功:




创建和构建工程

使用cocos2d-console这个工具来创建和构建工程,这个工具提供了下面几个功能:

  • new 创建一个新的工程
  • compile 编译当前工程,生成二进制文件
  • deploy 发布程序到一个平台
  • run 编译和发布,和运行程序


1、创建Cocos2d-x工程

打开cmd,输入cocos new 项目名 -p 包名 -l cpp -d 存放路径



这样代表我们成功创建了一个项目了,工程结构如下:



这里为了方便,将创建工程的命令写成一个批处理文件:

新建一个txt文本,打开,拷贝下面的代码到文本中

[plain]  view plain  copy
  1. @echo off  
  2. set /p projectName=please input project name:  
  3. set /p packageName=please input package name:  
  4. @echo cocos new %projectName% -p %packageName% -l cpp -d F:\Cocos2d-x\project  
  5. cocos new %projectName% -p %packageName% -l cpp -d F:\Cocos2d-x\project  
  6. @echo Create Success!!!  
  7. pause  
保存并重命名为create_project.bat,放到引擎根目录F:\Cocos2d-x中,以后要创建工程了,直接打开,输入项目名和包名即可,项目存放路径我默认的是F:\Cocos2d-x\project,你可以自己修改



2、构建Android工程

打开cmd,进入刚才创建的工程目录,输入cocos compile -p android -j 4



若看到下面几句,则代表编译成功


我们可以在F:\Cocos2d-x\project\HelloCocos2dx\publish\android看到apk文件,可以安装到手机上跑。


3、在Eclipse下运行程序

打开Eclipse,先设置SDK和NDK路径

工具栏Window->Preferences,选择Android节点,看SDK Location是否正确,我的是F:\Android\sdk

打开Android节点,找到NDK,看NDK Location是否正确,我的是F:\Android\android-ndk-r9b


接下来导入工程,选择File->import,打开导入对话框,选择Android\Existing Android Code Into Workspace,选中工程导进来:



导入之后,如果出现以下错误,就是你没有把cocos2d-x的包导入Eclipse工程里面:



这时候选中工程右键 -> Build Path -> Link Source -> Browser,添加F:\Cocos2d-x\cocos\platform\android\java\src到项目中。



运行该工程,右键该工程,选择Run as ->android application(从cocos2d-2.0-rc0a-x-2.0起,支持了open gl es2.0,默认的工程创建是要求运行于open gl es2.0上的,即目前模拟器还未支持,所以必须用真机去调试)。


https://blog.csdn.net/wxc237786026/article/details/32907079

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325603506&siteId=291194637