Creating multi-platform games with Cocos2d-x version 3.0 or later

Updated on 23-Apr-2015

In this tutorial you’ll learn how to create a simple game using the Cocos2d-x framework, version 3.0 or later, in a Windows* development environment and how to compile it to run on Windows and Android*.

What is Cocos2d-x?

Cocos2d-x is a cross-platform framework for games (and other graphical apps, like interactive books) based on the cocos2d for iOS*, but using C++, JavaScript*, or Lua* instead of Objective-C*.

One of the advantages of this framework is to create games that can be deployed on different platforms (Android, iOS, Win32, Windows* Phone, Mac*, Linux*, etc.), keeping the same code base with a few platform-specific adaptations for each one.

Cocos2d-x Console

The cocos2d-console was introduced in the 3.0 version. It is a command line tool that provides some functionality to manage Cocos2d-x or Cocos2d-JS projects like: creation, execution, building, debug, etc.

Creating your first game

1 – Download the latest version of the framework and unzip it in your development environment. In this tutorial the 3.3rc0 version was used, and the framework was unzipped to the desktop (C:\Users\intel-user\Desktop\cocos2d-x-3.3rc0).

2 – To create a new project in cocos2d-x, use setup.py (a Python* script) located in the framework folder to configure all the environment variables to build for Win32 and Android platforms. You will need to download, install and configure the items below before executing setup.py:

  • Android* SDK
  • Android* NDK
  • Apache Ant*

If you haven’t installed Python Runtime, download the 2.7.6 version from here:http://www.python.org/download/

3 – Open the command prompt (cmd.exe) and execute the following commands:

– Navigate to the script folder (framework folder):

cd C:\Users\intel-user\Desktop\cocos2d-x-3.3rc0

– Run the script setup.py:

python setup.py (or setup.py only)
Note: To run the Python command from the command prompt, add the folder where Python was installed to the environment variable path.
– The script will request the installation path of Android SDK, Android NDK, and ANT.

  • Android NDK folder path:

  • Android SDK folder path:

  • Apache ANT bin folder path:

After including the requested paths, reopen the command prompt (cmd.exe). This action is necessary to use cocos2d-console commands.

4 – Type cmd.exe to go to the command prompt (cocos2d-console commands can only be issued from here) and open the framework folder again:

cd C:\Users\intel-user\Desktop\cocos2d-x-3.3rc0

In the step below we will create a new Cocos2d-x project:

cocos new MyGame –p com.Project.MyGame –l cpp –d Project

Here are quick explanations of the parameters:
new: creates a new project and must be followed by the name of the project (in the example, MyGame)

  • -p: defines package name
  • -l: select programming language. The value can be cpp or lua
  • -d: directory where the framework will create the project structure

If everything went allright, your project will be created in the Project folder, in the directory where framework was extracted.

The created project contains the base code of the game (Classes), the resources (images, audio, etc.), and one project for each framework-supported platform.

Building Android* Apps
Requirements:

  • You need to configure all the environment variables for building Android app games (Android SDK, Android NDK, and ANT). If you have not done this step yet, please see the “Creating your first game” section of this article.
  • Java Development Kit (JDK) installed

Note: Cocos2d-console uses the javac command to build for Android, because of this is necessary add JAVA_HOME environment variable (JDK path).

1 – We are going to compile our game for more than one architecture, since the framework doesn’t compile for x86 and armeabi-v7a by default. Edit the Application.mk file located in:

C:\Users\intel-user\Desktop\cocos2d-x-3.3rc0\Project\MyGame\proj.android\jni

2 – Add the following line to the file:
APP_ABI := armeabi armeabi-v7a x86

Now that we added our targeted architectures, let’s compile our game!
3 – Using the command prompt, go to the framework folder:

cd C:\Users\intel-user\Desktop\cocos2d-x-3.3rc0

4 – Execute the command below to compile and run the game for Android.
cocos run –s Project\MyGame –p android

  • run: compile and run the project
  • -s: path to project folder
  • -p: selected platform

Note: To compile only, enter: cocos compile –s Project\MyGame –p android
If everything worked properly, the cocos2d-console command will use adb (if configured in environment variables) to install the APK file in a connected device or initialized emulator. If they aren’t available, the command will wait for a device or an emulator to become available, like the picture below:

If you have initialized an emulator or connected a device, this screen will appear:

Building Win32 Apps (for Windows* 7 or Windows* 8 desktop mode)

You will need Visual Studio* 2012 or later to build.
1 – Using the command prompt (cmd.exe), go to the folder where framework was extracted:

cd C:\Users\intel-user\Desktop\cocos2d-x-3.3rc0

2 – Execute the following command to compile and run the game on Windows:

cocos run –s Project\MyGame –p win32

Here are quick explanations of the parameters:

  • run: compile and run the selected project
  • -s: path to project folder
  • -p: selected platform

Note: To compile only, use “compile” instead of “run”, as follows:

cocos compile –s Project\MyGame –p win32

After executing the run command, you will see the following screen if everything worked ok:

It’s possible to use Visual Studio to compile and run the game project:

1 – Inside the Project directory, open with Visual Studio MyGame.sln file in the “proj.win32” folder.

2 – To compile the project, press F6 (or Build menu -> Build Solution) and press F5 to execute it (or Debug menu -> Start Debugging). After building and executing, you should see the same screen presented after the console steps.

Ok, now you know how to create your game and compile it for Android (x86 and ARM*), Windows 7, and Windows 8 (desktop mode)!

For more such Android resources and tools from Intel, please visit the Intel® Developer Zone

Source: https://software.intel.com/en-us/articles/creating-multi-platform-games-with-cocos2d-x-version-30-or-later

Connect On :