IOS创造cocos2d-x的游戏工程
初接触cocos2d,各种迷茫。
现在发现一些东西,
cocos2d-x 2.1.5之前可通过命令install-templates-xcode.sh来为xcode增加cocos2d模板,
但是
cocos2d-x 2.x之后就不能通过上述方式来创建工程了,因为下载的cocos2d源代码目录就已经没有那个文件了,见图
官方提供了跨平台的解决方案,使用python命令来创建一个多平台支持的多工程目录。
步骤:
1.在终端中进入cocos2d-x/tools/project-creator/的目录
2.使用命令python create_project.py -project HelloWorld -package com.bjmzs.org -language cpp
来创建cocos2d工程,工程见图
3.ok后,进入cocos2d-x/project/ 下找到刚刚创建的项目 进去,找到ios目录,通过xcod打开。点击编译并运行。
而现在出的cocos2d-x 3.x版本
首先,cocos2d提供了一个自动集成到xcode的应用程序,Cocos2D Installer 3.1.0.app。
官网地址:http://www.cocos2d-swift.org/download
地址:http://download.****.net/detail/love_monkey/7700395
下载这个软件后,我们就可以直接运行程序,成功后,xcode就集成了cocos2d 3.x模板,见图
它的框架中好像去掉了CCLayer,这个我在源代码中没有找到
layer scene的文件夹下没有CCLayer这个类了,全文也搜不到。
注意:这个下载的是cocos2d的版本,而不是cocos2d-x的版本,带不带x是有很大区别的,正是没发现这个区别,我才纳闷了为啥使用这个install集成的模板没有CCLayer类,原来他们是不同的。
下载的这个cocos2d是不跨平台的,是object-c代码编写的,
而cocos2d-x是跨平台的,是c++代码编写的。
话说,以前的cocos2d框架就没有CCLayer这个类么?不可能吧,很多讲述cocos2d的书都有介绍CCLayer啊。
经过我查找,官网上是没找到相关的东西,cocos2d源代码中倒是有2行,说是v3不在使用layer了,用node代替,擦,不学cocos2d了,直接学cocos2d-x吧。
使用cocos的sdk目录的方式来创建cocos2d工程。
1.cocos2d-x 3.x版本的sdk目录中的tools目录下又没有了create-project.py了,这样3版本的和原先的2版本的又不一样了。
2.官方提供的方案是,在根目录下有一个setup.py,这个文件是用来设置多平台开发cocos2d项目的,执行命令
python setup.py
Setting up cocos2d-x...
->Check environment variable COCOS_CONSOLE_ROOT
->Search for environment variable COCOS_CONSOLE_ROOT...
->COCOS_CONSOLE_ROOT is found : /Users/haiwang/study/IOSGame/cocos2d-x-3.2/tools/cocos2d-console/bin
->Configuration for Android platform only, you can also skip and manually edit "/Users/haiwang/.bash_profile"
->Check environment variable NDK_ROOT
->Search for environment variable NDK_ROOT...
->NDK_ROOT is found : /Users/haiwang/program/android-ndk-r10
->Check environment variable ANDROID_SDK_ROOT
->Search for environment variable ANDROID_SDK_ROOT...
->ANDROID_SDK_ROOT is found : /Users/haiwang/program/android-sdk
->Check environment variable ANT_ROOT
->Search for environment variable ANT_ROOT...
->ANT_ROOT is found : /Users/haiwang/program/apache-ant-1.9.3/bin
Please execute command: "source /Users/haiwang/.bash_profile" to make added system variables take effect
它会提示你应该去设置什么环境变量,jdk啊,android sdk啊,ndk啊,ant啊等等,都设置完了,保存到.base_profile中。设置完这么一大堆环境变量,为啥啊,就是为了直接用命令行来创建工程了,我们已经设置了cocos的home path,所以可以在任何path下来执行cocos命令。
尝试一下
haiwangdeMacBook-Pro:cocos2d-x-3.2 haiwang$ cocos --help
/Users/haiwang/study/IOSGame/cocos2d-x-3.2/tools/cocos2d-console/bin/cocos.py 0.7 - cocos console: A command line tool for cocos2d
Available commands:
run Compiles & deploy project and then runs it on the target
luacompile minifies and/or compiles lua files
deploy Deploy a project to the target
compile Compiles the current project to binary
new Creates a new project
jscompile minifies and/or compiles js files
Available arguments:
-h, --help Show this help information
-v, --version Show the version of this command tool
Example:
/Users/haiwang/study/IOSGame/cocos2d-x-3.2/tools/cocos2d-console/bin/cocos.py new --help
/Users/haiwang/study/IOSGame/cocos2d-x-3.2/tools/cocos2d-console/bin/cocos.py run --help
然后我们可以根据命令行提示,来创建新工程了。
cocos new
haiwangdeMacBook-Pro:cocos2d-x-3.2 haiwang$ cocos new
Running command: new
usage: cocos new [-h] [-p PACKAGE_NAME] -l {cpp,lua,js} [-d DIRECTORY]
[-t TEMPLATE_NAME] [--ios-bundleid IOS_BUNDLEID]
[--mac-bundleid MAC_BUNDLEID] [--no-native]
[PROJECT_NAME]
cocos new: error: argument -l/--language is required
haiwangdeMacBook-Pro:cocos2d-x-3.2 haiwang$ cocos new HelloCocosWorld -p com.hai.hellococosworld -l cpp
Running command: new
> Copy template into /Users/haiwang/study/IOSGame/cocos2d-x-3.2/HelloCocosWorld
> Copying cocos2d-x files...
> Rename project name from 'HelloCpp' to 'HelloCocosWorld'
> Replace the project name from 'HelloCpp' to 'HelloCocosWorld'
> Replace the project package name from 'org.cocos2dx.hellocpp' to 'com.hai.hellococosworld'
创建后的工程见图
然后我们准备运行这个工程,在这里要注意,需要cd到项目目录下
haiwangdeMacBook-Pro:cocos2d-x-3.2 haiwang$ cocos run
Running command: compile
Can't find config file .cocos-project.json in path /Users/haiwang/study/IOSGame/cocos2d-x-3.2
haiwangdeMacBook-Pro:cocos2d-x-3.2 haiwang$ cocos run
Running command: compile
Can't find config file .cocos-project.json in path /Users/haiwang/study/IOSGame/cocos2d-x-3.2
haiwangdeMacBook-Pro:cocos2d-x-3.2 haiwang$ ls
AUTHORS cocos plugin
CHANGELOG docssetup.py
CMakeLists.txt download-deps.pytemplates
HelloCocosWorld extensionstests
README.md externaltools
build licenses
haiwangdeMacBook-Pro:cocos2d-x-3.2 haiwang$ cd HelloCocosWorld/
haiwangdeMacBook-Pro:HelloCocosWorld haiwang$ ls
CMakeLists.txt Resourcesproj.androidproj.linuxproj.wp8-xaml
Classes cocos2dproj.ios_macproj.win32
haiwangdeMacBook-Pro:HelloCocosWorld haiwang$ cocos run
Running command: compile
The target platform is not specified.
You can specify a target platform with "-p" or "--platform".
Available platforms : ['android', 'ios', 'mac']
haiwangdeMacBook-Pro:HelloCocosWorld haiwang$ cocos run -p ios
Running command: compile
Building mode: debug
building
running: 'xcodebuild -project "/Users/haiwang/study/IOSGame/cocos2d-x-3.2/HelloCocosWorld/proj.ios_mac/HelloCocosWorld.xcodeproj" -configuration Debug -target "HelloCocosWorld iOS" -arch i386 -sdk iphonesimulator CONFIGURATION_BUILD_DIR=/Users/haiwang/study/IOSGame/cocos2d-x-3.2/HelloCocosWorld/bin/debug/ios'
Build settings from command line:
ARCHS = i386
CONFIGURATION_BUILD_DIR = /Users/haiwang/study/IOSGame/cocos2d-x-3.2/HelloCocosWorld/bin/debug/ios
SDKROOT = iphonesimulator7.1
这里就有个疑问了,我当初在搭建IOS的jenkins可持续集成环境的时候,想让jenkins使用命令行来使得模拟器安装并运行一个ipa程序,但是没有方法,xcode没有提供此类命令,而且系统也没找到这样的解决方案,现在cocos2d的就实现了此效果,得看看怎么回事。