-
Book Overview & Buying
-
Table Of Contents
Cocos2d-x cookbook
By :
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.
cocos command help by executing it with the --help parameter, as follows:$ cocos --help
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:

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.
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.cocos run –help for more detailed information.The result of this command is shown in the following screenshot:


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.
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:
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]
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]
The run command continues to compile and deploy commands.
Change the font size
Change margin width
Change background colour