Click here to Skip to main content
Click here to Skip to main content

Creating multi-platform games with Cocos2d-x

In this tutorial how to create a simple game using the Cocos2d-x framework in a Windows development environment and how to compile it to run on Windows 8 and Android.

Editorial Note

This article is in the Product Showcase section for our sponsors at CodeProject. These reviews are intended to provide you with information on products and services that we consider useful and of value to developers.

Introduction

In this tutorial how to create a simple game using the Cocos2d-x framework in a Windows development environment and how to compile it to run on Windows 8 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, Windows* 8, Mac*, Linux*, etc.) keeping the same code base and making a few platform-specific adaptations for each one.

The source code of the framework is granted under the MIT License, and it be can be found here.

If you want to know more about Cocos2d-x and its documentation, check out: http://www.cocos2d-x.org/.

Creating your first game

1. Download the latest version of the framework from the site and unzip it in your development environment. In this tutorial, the version 2.2.2 was used, and the framework was unzipped to the Desktop (C:\Users\felipe.pedroso\Desktop\cocos2d-x-2.2).

2. To create a new project on cocos2d-x, we are going to use a Python* script (create_project.py) that creates the whole project structure inside the folder where the framework was unzipped. If you don’t have the Python runtime installed, download the 2.7.6 version from this link: http://www.python.org/download/.

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

  • Go to the script folder (it’s important to run the script inside the ‘project-creator’ folder)
    cd C:\Users\felipe.pedroso\Desktop\cocos2d-x-2.2\tools\project-creator
  • Run the script with the following command:
    python create_project.py -project MyFirstGame -package com.example.myfirstgame -language cpp

The parameters are as follows:
project: The name of your project/game
package: The package name of your app (e.g., com.myCompany.MyFirstGame)
language: The programming language of the project (cpp, lua and JavaScript)

Note: To run the python command from the command prompt, add the folder where Python was installed to the environment variable path.

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

Building as a Win32 App (Windows* 7 or Windows 8 desktop mode)

Requirements:

1. Open the MyFirstGame.sln file inside the proj.win32 folder from the project directory using Visual Studio.

2. Build the project by pressing F6 (or use the menu Build -> Build Solution) and run the project pressing F5 (or use the menu Debug->Start Debugging).

If nothing went wrong, you’ll see the following window:

Building as a Windows Store App

Requirements:

To build your project as a Windows Store App, open the MyFirstgame.sln file inside the proj.winrt folder and build it using the same procedure that was used on the Win32 project.

After building and running, you’ll see the following screen:

Note: the cocos2d-x used in this tutorial didn’t work with Windows* 8.1.

Building as a Android App

Requirements:

In the same way that Python was added to the Windows path, add the directories, tools, and platform-tools from the Android SDK, the root directory from NDK, and the bin directory from Apache Ant in order to use them to build your app.

1. Open a new command prompt (cmd.exe) and run the following commands to configure the environment variables that are necessary to compile the Android app:

set COCOS2DX_ROOT=C:\Users\felipe.pedroso\Desktop\cocos2d-x-2.2

        set NDK_TOOLCHAIN_VERSION=4.8

        set NDK_MODULE_PATH=%COCOS2DX_ROOT%;%COCOS2DX_ROOT%\cocos2dx\platform\third_party\android\prebuilt


The variables we used are:

COCOS2DX_ROOT: the directory where the framework was unzipped NDK_TOOLCHAIN_VERSION: the version of the NDK toolchain that will be used to build the project
NDK_MODULE_PATH: the modules that need to be included on the NDK build. In this case, we are using the prebuilt modules from cocos2d-x

2. With the environment variables configured, go to the Android project folder:

cd C:\Users\felipe.pedroso\Desktop\cocos2d-x-2.2\projects\MyFirstGame\proj.android

3. Copy the game resources (images, sounds, etc.) to the assets folder:

rmdir /S /Q assets

        mkdir assets

        xcopy /E ..\Resources .\assets

4. Run the following command to build the native modules:

ndk-build.cmd -C . APP_ABI="armeabi armeabi-v7a x86"

This command will generate the native libraries for three different architectures: ARM, ARM-NEON*, and x86. This will allow your game to run on these architectures taking the best from them.

5. After finishing the build process, build the Android app with the ant command:

ant debug



Now, to install the app in a device or emulator use the command:

adb install -r bin\MyFirstGame.apk

After that, you just need to run your app:

OK, now your game can run on at least three platforms: Android, Windows 7, and Windows 8!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

FELIPE PEDROSO

United States United States
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 28 May 2014
Article Copyright 2014 by FELIPE PEDROSO
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid