Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Cocos2d-x cookbook

You're reading from   Cocos2d-x cookbook Over 50 hands-on recipes to help you efficiently administer and maintain your games with Cocos2d-x

Arrow left icon
Product type Paperback
Published in Nov 2015
Publisher
ISBN-13 9781783284757
Length 254 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Akihiro Matsuura Akihiro Matsuura
Author Profile Icon Akihiro Matsuura
Akihiro Matsuura
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Getting Started with Cocos2d-x FREE CHAPTER 2. Creating Sprites 3. Working with Labels 4. Building Scenes and Layers 5. Creating GUIs 6. Playing Sounds 7. Working with Resource Files 8. Working with Hardware 9. Controlling Physics 10. Improving Games with Extra Features 11. Taking Advantages 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.

You have been reading a chapter from
Cocos2d-x cookbook
Published in: Nov 2015
Publisher:
ISBN-13: 9781783284757
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image