Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Cocos2d-x cookbook
  • Table Of Contents Toc
Cocos2d-x cookbook

Cocos2d-x cookbook

By : Akihiro Matsuura
4.6 (5)
close
close
Cocos2d-x cookbook

Cocos2d-x cookbook

4.6 (5)
By: Akihiro Matsuura

Overview of this book

Cocos2d-x is a suite of open source, cross-platform game-development tools used by thousands of developers all over the world. Cocos2d-x is a game framework written in C++, with a thin platform-dependent layer. Completely written in C++, the core engine has the smallest footprint and the fastest speed of any other game engine, and is optimized to be run on all kinds of devices. You will begin with the initial setup and installation of Cocos2d before moving on to the fundamentals needed to create a new project. You will then explore and create the sprites, animations, and actions that you will include in the game. Next you will look at strings and create labels, including a label with True Type Font (TTF) font support. Later, you will learn about layer and scene creation and transition. Then you will create the GUI parts essential for a game, such as buttons and switches. After that, you will breathe life into the game with background music and sound effects using the all new Cocos2d-x audio engine. You will then discover how to manage resource files and implement processes that depend on the operating system. Finally, you will polish your game with physics such as gravity and elevation, learn about tools that will help you make games more robust and stable, and get to know best practices to improve the game you have developed.
Table of Contents (13 chapters)
close
close
12
Index

Using the Cocos command

The next step is using the cocos command. It is a cross-platform tool with which you can create a new project, build it, run it, and deploy it. The cocos command works for all Cocos2d-x supported platforms and you don't need to use an IDE if you don't want to. In this recipe, we take a look at this command and explain how to use it.

How to do it...

  1. You can use the cocos command help by executing it with the --help parameter, as follows:
    $ cocos --help
    
  2. We then move on to generating our new project:

    First, we create a new Cocos2d-x project with the cocos new command, as shown here:

    $ cocos new MyGame -p com.example.mygame -l cpp -d ~/Documents/
    

    The result of this command is shown the following screenshot:

    How to do it...

    Behind the new parameter is the project name. The other parameters that are mentioned denote the following:

    • MyGame is the name of your project.
    • -p is the package name for Android. This is the application ID in the Google Play store. So, you should use the reverse domain name as the unique name.
    • -l is the programming language used for the project. You should use cpp because we will use C++ in this book.
    • -d is the location in which to generate the new project. This time, we generate it in the user's documents directory.

    You can look up these variables using the following command:

    $ cocos new —help
    

    Congratulations, you can generate your new project. The next step is to build and run using the cocos command.

  3. Compiling the project:

    If you want to build and run for iOS, you need to execute the following command:

    $ cocos run -s ~/Documents/MyGame -p ios
    

    The parameters that are mentioned are explained as follows:

    • -s is the directory of the project. This could be an absolute path or a relative path.
    • -p denotes which platform to run on. If you want to run on Android you use -p android. The available options are IOS, Android, Win32, Mac, and Linux.
    • You can run cocos run –help for more detailed information.

    The result of this command is shown in the following screenshot:

    How to do it...
  4. You can now build and run iOS applications on cocos2d-x. However, you have to wait for a long time if this is your first time building an iOS application. It takes a long time to build a Cocos2d-x library, depending on if it was a clean build or a first build.
    How to do it...

How it works...

The cocos command can create a new project and build it. You should use the cocos command if you want to create a new project. Of course, you can build using Xcode or Eclipse. You can easily develop and debug using these tools.

There's more...

The cocos run command has other parameters. They are the following:

  • --portrait will set the project as a portrait. This command has no argument.
  • --ios-bundleid will set the bundle ID for the iOS project. However, it is not difficult to set it later.

The cocos command also includes some other commands, which are as follows:

  • The compile command: This command is used to build a project. The following patterns are useful parameters. You can see all parameters and options if you execute the cocos compile [–h] command:
    cocos compile [-h] [-s SRC_DIR] [-q] [-p PLATFORM] [-m MODE]
    
  • The deploy command: This command only takes effect when the target platform is Android. It will re-install the specified project to the android device or simulator:
    cocos deploy [-h] [-s SRC_DIR] [-q] [-p PLATFORM] [-m MODE]
    

    Tip

    The run command continues to compile and deploy commands.

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Cocos2d-x cookbook
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon