This tutorial will walk you through how to successfully create a new game project using Cocos 2d-x.

Step 1: Open the Command Prompt

To access the Command Prompt, go to your computer’s Start Menu. In the Search Programs and Files box, type cmd. This will open a window like this:

cmd

Step 2: Cd Into Your Cocos 2d-x Directory

Type cd and then the path of the Cocos 2d-x directory you installed. (See here for How to Install Cocos 2d-x on Windows.)

To find the path of your Cocos 2d-x directory, go to your computer’s Start Menu. In the Search Programs and Files box, type cocos2d-x. Click on the folder with the same name (it will probably have a version number on the end: e.g. cocos2d-x-3.4). This will take you to the directory (folder) containing your Cocos 2d-x folder. Click on that folder so you’re inside it.

Cocos 2dx Folder - Inside

At the top where it says cocos2d-x-3.4 (or whatever your version number is), right-click and select Copy Address. The address is the path of your Cocos 2d-x files.

E.g. C:\Users\HP\Documents\3 – Mobile Development\cocos2d-x-3.4

Now that you’ve found the path of your Cocos 2d-x files, either type it or paste it into the command prompt by right-clicking and then selecting paste. (Note that you cannot use Ctrl-V to paste within the Command Prompt). Then hit the Enter key.

If you get an error, make sure you typed cd Your_Cocos_Path, not just the path by itself.

Step 3: Run Setup

Type python setup.py into the Command Prompt. Ignore its request to restart the Command Prompt unless you had to provide path names for NDK, SDK, or ANT. (Ideally, you should run setup, the Command Prompt should do its thing, and then return a new line for you to type on. You’ll know you’re on a new line because you’ll see the path you typed in followed by a >.)

setup

Step 4: Create Your Project

On the new line in the Command Prompt, type:

cocos new GameName -p com.CompanyName.GameName -l cpp -d Directory_Path

GameName is pretty self explanatory. It’s the name you’re going to call your game.

CompanyName is the name of the company you’re going to publish you’re game under. This is needed for Android. If you don’t have a name yet, just make one up.

-p, -l, and -d are flags that signals to cocos you’re about to provide special information it needs to set up your game.

  • -p com.CompanyName.GameName: This is Android’s name for your package (game)
  • -l cpp: “-l” is actually an L for language. It’s the language you’re going to code your cocos game in (e.g. C++).
  • -d Directory_Path: This is the path of the directory (folder) you want your game to be saved in. The result will be a new folder with path Directory_Path\GameName.

Command Prompt may take a couple of minutes to create your new game. Once it finishes, if you navigate through your computer’s folders (as given by Directory_Path), you’ll see that a new folder called GameName has been created. If you go inside the GameName folder, you’ll find it contains files that look like this:

Inside New Cocos Project

Step 5: Build and Run Your Project

On the new line in the Command Prompt, type:

cocos run -s Directory_Path\GameName -p ChosenPlatform

where ChosenPlatform is either win32, web, mac, linux, android, ios, wp8, wp8_1, or metro. (Note that these options may be limited depending on your system.)

It will take several minutes to compile. When it’s done, you should get a window like this:

Hello World

You can accomplish the same thing by opening your project in Visual Studio. The Microsoft Visual Studio file has the same name as your game and can be found within GameName\proj.win8.1-universal, GameName\proj.win32, or GameName\proj.wp8-xaml (choose the one that matches your OS).

Possible Errors:

cocos is not recognized as an internal or external command
— Go into your cocos 2d-x directory and run python setup.py again

Can't find config file .cocos-project.json in path
— .cocos-project.json is a hidden file within your project’s directory. Sometimes, it can get deleted if the compilation process gets messed up. If that happens, create a new project and copy its .cocos-project.json file into your original project and then try recompiling. Alternatively, you can just create a .cocos-project.json file yourself. All it contains is the following code: { "project_type": "cpp" }

NDK will break during compile if you have a space in your path. If that happens, close the Command Prompt, modify the folder/file name it broke on so it doesn’t have a space, then try again.

-- waiting for device --
— When compiling for android, this error means you don’t have a default android emulator installed. Instead, you have to launch one each time. If you haven’t created an emulator yet, type android to launch the AVD Manager.

Android SDK Manager

At the top, click on Tools -> Manage AVDs.

AVD Manager

Then create a new device. I recommend giving it an easy name. Once that’s done, go to the Command Prompt. Type emulator @MyDeviceName. Wait for the android device to launch and the lock screen to show. It usually takes a minute for the device to launch but up to fifteen minutes for the word “Android” to disappear from the device and the lock screen to show. Once it does, use your mouse to swipe across it and unlock the device, then run your cocos program like normal. It should compile fine now.

From now on, whenever you want to run a program for the android platform, just make sure you’ve run the emulator @MyDeviceName command first.

Android Emulator: Not Ready vs. Half-Ready (Locked) vs. Ready (Unlocked)

Android - Not ReadyAndroid - Ready