- Install Xcode
- Install Cocos2d-x 4.0
- Create a new Cocos2d-x project
- Compile and run for iOS
- Compile and run for macOS
As I mentioned in the previous post, I am going to use Cocos2d-x for my game engine. I will install it and make sure it works under different operating systems (macOS, Windows, Linux) and also compiles for different platforms (minus the Android).
In this post I will go through the steps I took to install it on my Macbook Pro!
Install Xcode
First, install Xcode, the native IDE for programming for macOS and iOS.
- Download and install Xcode from the App Store. It is the Apple’s free programming IDE and compiler.
- Now in a new terminal install the Xcode command line tools. The command line tools are not directly necessary to make and compile a Cocos2d-x projects, but it will come handy later in different occasions which you will see later.
xcode-select --install
At this point you can already compile for iOS devices. However to be able to compile for Android devices, You need to install some extra Android related stuff. It includes Apache Ant and Android Studio (and the packages that come with it: Android SDK and Android NDK).
Install Cocos2d-x 4.0
Now that Xcode is installed, it is time to install Cocos2d-x itself:
- Installing Cocos2d-x needs Python 2.7x installed which comes natively on macOS. Check your python version in terminal to see if you have the right version. Mine is 2.7.16.
python --version
- Download Cocos2d-x (cocos2d-x-4.0.zip) from www.cocos2d-x.org and unzip it in a folder that you want it to reside e.g. /Applications/Dev/
- Setup Cocos2d-x by running the setup.py in a terminal from inside of the unzipped folder e.g. /Applications/Dev/cocos2d-x-4.0/
python setup.py
- It sets up some environment variables first as below and then asks for some paths.
- COCOS_CONSOLE_ROOT
- COCOS_X_ROOT
- COCOS_TEMPLATES_ROOT
- If you have Android Studio and Apache Ant installed in your system (in my case they are installed in Dev folder):
- For NDK_ROOT question, put /Users/rezagn/Library/Android/sdk/ndk-bundle
- For ANDROID_SDK_ROOT question, put /Users/rezagn/Library/Android/sdk
- For ANT_ROOT question, put /Applications/Dev/apache-ant-1.10.7/bin
After going through the steps I mentioned above, I have these lines added to my ~/.profile settings file:
# Add environment variable COCOS_CONSOLE_ROOT for cocos2d-x export COCOS_CONSOLE_ROOT=/Applications/Dev/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="/Applications/Dev" export PATH=$COCOS_X_ROOT:$PATH # Add environment variable COCOS_TEMPLATES_ROOT for cocos2d-x export COCOS_TEMPLATES_ROOT=/Applications/Dev/cocos2d-x-4.0/templates export PATH=$COCOS_TEMPLATES_ROOT:$PATH # Add environment variable NDK_ROOT for cocos2d-x export NDK_ROOT="/Users/rezagn/Library/Android/sdk/ndk-bundle" export PATH=$NDK_ROOT:$PATH # Add environment variable ANDROID_SDK_ROOT for cocos2d-x export ANDROID_SDK_ROOT="/Users/rezagn/Library/Android/sdk" export PATH=$ANDROID_SDK_ROOT:$PATH export PATH=$ANDROID_SDK_ROOT/tools:$ANDROID_SDK_ROOT/platform-tools:$PATH # Add environment variable ANT_ROOT for cocos2d-x export ANT_ROOT="/Applications/Dev/apache-ant-1.10.7/bin" export PATH=$ANT_ROOT:$PATH
If you don’t have these lines, add them to your setup file manually. Remember to replace my username “rezagn” with yours and make sure the paths are correct.
Create a new Cocos2d-x project
To make new projects, we will use the cocos command in the terminal. If you just type cocos and press Enter, you will get the command line options help.
~/Projects/games > cocos /Applications/Dev/cocos2d-x-4.0/tools/cocos2d-console/bin/cocos.py 2.3 - cocos console: A command line tool for Cocos2d-x. Available commands: run Compiles, deploy and run project on the target. luacompile Encrypt and/or compile lua files. deploy Compile and deploy a project to a device/simulator. compile Compile projects to binary. gen-simulator Generate Cocos Simulator. new Creates a new project. jscompile Compile and/or compress js files. Available arguments: -h, --help Show this help information. -v, --version Show the version of this command tool. --ol ['en', 'zh', 'zh_tr'] Specify the language of output messages. --agreement ['y', 'n'] Skip the agreement with specified value. Example: cocos new --help cocos run --help
Create a new project by typing the full command as bellow. Feel free to change the folder as you wish.
cocos new install_test -p com.games.install_test -l cpp -d ~/Projects/
This will make a project called “install_test” with all the necessary files and sub folders of the Cocos2d-x project in your specified folder!
Since the new release of Cocos2d-x v4.0, CMake is used to create projects and compile under different platforms. That means that the Cocos2d-x projects won’t come with an Xcode project file like in the earlier versions. We need to make two different Xcode project files for our newly built Cocos2d-x project which we called “install_test”. One for macOS and one for iOS.
To create an Xcode project for macOS, run these commands in Terminal from inside the “install_test” project folder.
mkdir build_mac && cd build_mac cmake .. -GXcode open install_test.xcodeproj
To create an Xcode project for iOS, run these commands in Terminal from inside the “install_test” project folder.
mkdir build_ios && cd build_ios cmake .. -GXcode -DCMAKE_SYSTEM_NAME=iOS -DCMAKE_OSX_SYSROOT=iphoneos open install_test.xcodeproj
Compile and run for iOS
- Run Xcode.
- In Xcode open the project file from “ios-build” folder of the project.
- Select an iOS device from the top left. e.g. iPhone 11 Pro Max.
- From the menu go to Product->Run.
- Xcode should build the project with no problems and run it in a simulator window.
Compile and run for macOS
Considering that compiling and running for iOS needs the simulator, it might be easier and faster to do your test and development for macOS instead. To compile for macOS:
- Run Xcode.
- In Xcode open the project file from “proj.ios_mac” folder of the newly created test project.
- Select “test-desktop.app” and “My Mac” as target project and device.
- From menu go to Build->Run the same as you did for iOS and this should run the game.
Amir J says
Hi Reza, I enjoyed your article,
Is there a reason you chose to use cocos2d instead of the SpriteKit? It’s help if you explains that…
A
Reza Ghobadinic says
Thanks for reading Amir 🙂
I added a line in the second post for it, but the main reason is that it is multi platform and the second reason is that it is C++, not Java, Swift or Objective-C!
When I made the first game with Cocos2d (iPhone version) with Objective-C, Sprite Kit didn’t exist yet.
Evgeniy says
Hi, Reza,
Can you, please, explane how can I make my folders structure in the folder of my source files?
In Cocos 3.17 folder has name “Classes”, I see in your example in Cocos4 it called “SourceFiles”.
Can you please share the link on some project with complicated structure?
If I put all file in the root of this folder – no problem, but it is not convenient.
On Android I should write path to each header file as workaround.
But it doesn’t work on ios.
Do you know, how we can have crossplatform project Android/ios with my own structure of my files on Cocos 3.17 or 4 ?
Reza Ghobadinic says
Hi Evgeniy,
The directories are the same in both v3 and v4 and it is called classes if you check in Finder. In the Xcode project on the other hand, you have Classes in v3 and Source Files in v4, but inside Source Files you can see there is still Classes and the files you write goes there.
The Xcode folders are not the same as the actual file folders. I would save all my code files in the directory “Classes” and in Xcode you can just organize them the way you want.
Does it answer your question?
VietThang says
I want to install Cocos2d-x-4.0 for Android, certainly Android Studio. What can I do?
Bruce Cichowlas says
I am having trouble with the cmake step and, in general, having trouble getting a xcodeproj file in my iOS Cocos2d-4 project. Maybe it is because I am using Xcode 12. Here’s what I get:
Check dependencies
target specifies product type ‘com.apple.product-type.tool’, but there’s no such product type for the ‘iphoneos’ platform
** BUILD FAILED **
Bruce Cichowlas says
I found that most of my problems were due to having accidentally corrupted a cmake file in the cocos2d-x directory structure. Finding that solved a lot of things!
I’m using Xcode 12 and Mac Big Sur (beta). I did find that I had to delete the lines about /usr/lib/libz.dylib and /usr/lib/libiconv.dylib in the build settings, because my /usr/lib did not contain them and apparently security on Big Sur makes it difficult to add anything to /usr/lib . I had to add a step to the build process to be able to select and get these libraries from the xcode distribution.
I was glad to get it working, but disappointed to find that if I moved the project to another directory, it no longer built properly. This is due to having some absolute path references in the generated cmake files.
It’s a problem for me because my ultimate goal is to upgrade cocos-2d (3.14) to cocos2d-x for a free music game that I provide and support for iOS. (Also for Android, though I use libgdx as my graphics engine there.). I get errors about multiple getPosition calls and other things when I build the cocos2d version now, so I thought it was a good time to upgrade to cocos2d-x, particularly since there might be some performance gains. So I’m now looking for a tutorial that talks about adding cocos2d-x to an existing iOS project.
Reza Ghobadinic says
Hey, I am glad you have figured out the problem. Unfortunately I don’t have Big Sur yet to test.
Dkr says
Does not work on iMac M1 BigSur
Reza Ghobadinic says
What happens? Where does it error? Can you give me more detail please? I don’t use an M1 Mac and it works on my Intel Mac with BugSur.