Ubuntu18.04和Android Studio搭建Cocos2d开发环境

网友投稿 747 2022-05-30

搭建步骤

第一步:下载Cocos2d

第二步:解压并安装Cocos2d

第三步:设置Cocos2d

第四步:使用cocos命令创建游戏项目

-:http://www.cocos2d-x.org/download

本例下载的版本是cocos2d-x-4.0

注意:不同版本的cocos2d的配置过程可能不一样,可以参考解压文档里的README.md文件。

下载的文档是zip格式,可以轻松地将其解压到某个目录下

安装Cocos2d

// 赋予install-deps-linux.sh执行权限 ~/Desktop/coco2dx/cocos2d-x-4.0$ sudo chmod 754 install-deps-linux.sh // 安装Cocos2d ~/Desktop/coco2dx/cocos2d-x-4.0$ ./install-deps-linux.sh

1

2

3

4

~/Desktop/coco2dx/cocos2d-x-4.0$ python setup.py

1

完整的配置过程:

~/Desktop/coco2dx/cocos2d-x-4.0$ python setup.py Setting up cocos2d-x... ->Check environment variable COCOS_CONSOLE_ROOT ->Search for environment variable COCOS_CONSOLE_ROOT... ->COCOS_CONSOLE_ROOT not found -> Add COCOS_CONSOLE_ROOT environment variable... ->Added COCOS_CONSOLE_ROOT=/home/kyun/Desktop/coco2dx/cocos2d-x-4.0/tools/cocos2d-console/bin ->Check environment variable COCOS_X_ROOT ->Search for environment variable COCOS_X_ROOT... ->COCOS_X_ROOT not found -> Add COCOS_X_ROOT environment variable... ->Added COCOS_X_ROOT=/home/kyun/Desktop/coco2dx ->Check environment variable COCOS_TEMPLATES_ROOT ->Search for environment variable COCOS_TEMPLATES_ROOT... ->COCOS_TEMPLATES_ROOT not found -> Add COCOS_TEMPLATES_ROOT environment variable... ->Added COCOS_TEMPLATES_ROOT=/home/kyun/Desktop/coco2dx/cocos2d-x-4.0/templates ->Configuration for Android platform only, you can also skip and manually edit "/home/kyun/.bashrc" ->Check environment variable NDK_ROOT ->Search for environment variable NDK_ROOT... ->NDK_ROOT not found ->Search for command ndk-build in system... ->Command ndk-build not found ->Please enter the path of NDK_ROOT (or press Enter to skip):/home/kyun/Android/Sdk/ndk/16.1.4479499 -> Add NDK_ROOT environment variable... ->Added NDK_ROOT=/home/kyun/Android/Sdk/ndk/16.1.4479499 ->Check environment variable ANDROID_SDK_ROOT ->Search for environment variable ANDROID_SDK_ROOT... ->ANDROID_SDK_ROOT is found : /home/kyun/Android/Sdk A backup file "/home/kyun/.bashrc.backup" is created for "/home/kyun/.bashrc". Please execute command: "source /home/kyun/.bashrc" to make added system variables take effect

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

Ubuntu18.04和Android Studio搭建Cocos2d开发环境

37

38

39

40

41

42

43

44

45

46

在配置的过程中,它会要求设置NDK的路径:

>Please enter the path of NDK_ROOT (or press Enter to skip):/home/kyun/Android/Sdk/ndk/16.1.4479499

1

本例输入/home/kyun/Android/Sdk/ndk/16.1.4479499,再按回车即可。

同时还会要求设置ANDROID_SDK_ROOT环境变量,值是android sdk的路径。这些配置都是写在/home/kyun/.bashrc这个脚本里。如果设置后,.bashrc脚本的最后就会有这些配置信息:

# Add environment variable COCOS_CONSOLE_ROOT for cocos2d-x export COCOS_CONSOLE_ROOT="/home/kyun/Desktop/coco2dx/cocos2d-x-4.0/tools/cocos2d-console/bin" export PATH=$COCOS_CONSOLE_ROOT:$PATH # Add environment variable COCOS_X_ROOT for cocos2d-x export COCOS_X_ROOT="/home/kyun/Desktop/coco2dx" export PATH=$COCOS_X_ROOT:$PATH # Add environment variable COCOS_TEMPLATES_ROOT for cocos2d-x export COCOS_TEMPLATES_ROOT="/home/kyun/Desktop/coco2dx/cocos2d-x-4.0/templates" export PATH=$COCOS_TEMPLATES_ROOT:$PATH # Add environment variable ANDROID_SDK_ROOT for cocos2d-x export ANDROID_SDK_ROOT=/home/kyun/Android/Sdk export PATH=$ANDROID_SDK_ROOT:$PATH # Add environment variable NDK_ROOT for cocos2d-x export NDK_ROOT="/home/kyun/Android/Sdk/ndk/16.1.4479499" export PATH=$NDK_ROOT:$PATH

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

使用source命令让脚本生效:

~/Desktop/coco2dx/cocos2d-x-4.0$ source /home/kyun/.bashrc

1

$ cocos new MyGame -p com.your_company.mygame -l cpp -d NEW_PROJECTS_DIR

1

如:

~/Desktop/Games$ cocos new MyGame -l cpp -p com.wong.game -d Games

1

结果:

~/Desktop/Games$ ls MyGame ~/Desktop/Games$ cd MyGame ~/Desktop/Games/MyGame$ ls Classes cocos2d proj.ios_mac proj.win32 CMakeLists.txt proj.android proj.linux Resources

1

2

3

4

5

6

7

运行cmake,生成makefile文件

~/Desktop/Games/Games/FirstGame$ mkdir -p build/linux-build ~/Desktop/Games/Games/FirstGame$ cd build/linux-build ~/Desktop/Games/Games/FirstGame$ cmake ../..

1

2

3

运行make命令编译

~/Desktop/Games/Games/FirstGame/build/linux-build$ make -j 4

1

运行

~/Desktop/Games/Games/FirstGame/build/linux-build$ cd bin ~/Desktop/Games/Games/FirstGame/build/linux-build/bin$ ./FirstGame/FirstGame

1

2

上面是在ubuntu18.04编译运行的。在不同平台上也是这样子去编译的。

使用android studio开发

使用Android Studio导入~/Desktop/Games/Games/FirstGame/proj.android,即可直接使用Android Studio开发。导入步骤:New ->Import Project…

Android Android Studio Cocos2D

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:【docker系列】镜像分层原理及容器层写时复制
下一篇:【云速建站】一键生成手机页
相关文章