Skip to content

2. Setting up a New Project

fyf edited this page Aug 21, 2014 · 1 revision

Setting up a New Project

This section will guide you through the process of setting up a new project with CocosBuilder. The process is slightly different depending on if you will be using Cocos2d for JavaScript, cocos2d-iphone or cocos2d-x.

Creating a Project for Cocos2d for JavaScript

To start a new Cocos2d for JavaScript project, simply select New Project… from the File menu. Choose a name for your project and save it. Your new project will be setup with an example MainScene.ccb file and MainScene.js script file. To try it out, either run it in CocosPlayer by selecting Run in CocosPlayer from the File menu or publishing it by selecting Publish. If you publish the files you can test it in your browser by double clicking the index.html file in the Published-HTML5 directory.

Creating a Project for cocos2d-iphone

It's best to setup your Xcode project before creating your CocosBuilder project. Make sure that the cocos2d templates are installed and create a new cocos2d-iphone project in Xcode (if you do not already have a project you want to use with CocosBuilder).

Once you have the Xcode project set up, create your CocosBuilder project. Select New Project… from the File menu. Save the CocosBuilder project in the Xcode project's Resources directory.

All images and sprite sheets you put in the Resources directory will now show up automatically in CocosBuilder's project view on the left side of the main window, but remember that you will need to add them to your Xcode project if you want to use them in the compiled version of your app.

Now update the settings of your project. Choose Project Settings…, in the settings window deselect the JavaScript based project checkbox. Save your changes and open the Publish Settings…. Set the iOS export path to your Resources directory, check the Flatten paths when publishing checkbox and check the Only publish ccb-files checkbox. Make sure that the iOS export target is selected, but the Android and HTML5 ones are unchecked.

Note: The settings described above assumes that you are managing all the resources yourself, and that you are using yellow folders (groups) in Xcode.

Publishing Your Project

Before you can import the files you create in CocosBuilder in your code you will need to publish them. To publish all ccb-files in a project, select Publish in the File menu. For each ccb-file a new ccbi-file is created. The ccbi files are very compact binary files with all unnecessary information stripped out. Include the ccbi-files in your Xcode project.

To load a ccbi-file from your code, make sure that you have included the CCBReader classes in your Xcode project. Then call the CCBReader's nodeGraphFromFile method.

CCNode* myNodeGraph = [CCBReader nodeGraphFromFile:@"myFile.ccbi"];

Project Settings

By default CocosBuilder will export your ccb-files to the same directory as the project file, but you can change in which directories CocosBuilder should look for resources and to where ccb-files should be exported. It is also possible to export ccb-files together with your images to a zip-file.

Access the project settings by selecting Project Settings… from the File menu.

Publish to zip-file

You have the option to publish to a zip file. If this option is used a zip file called ccb.zip will be placed in your destination directory. To be able to load the zip-file in your code you need to:

  1. Include the SSZipArchive classes, in addition to the CCBReader, in your Xcode project.
  2. In your project's build settings, add CCB_ENABLE_UNZIP to your Preprocessor Macros.
  3. Replace cocos2d's default CCFileUtils class by calling [CCBFileUtils sharedFileUtils] before your cocos2d is loaded (you will need to import CCBReader.h to do this).
  4. Unpack the zip file by calling [CCBReader unzipResources:@"ccb.zip"]

Flatten file paths

In the project settings, you can choose to flatten file paths (this is the default setting). If you do this, all file paths in the exported ccbi-files will be flattened (e.g. mySubDirectory/myImage.png will be transformed to myImage.png). If you use the zip option in combination, the files will be copied to a single directory within the zip file. If you are publishing without the zip option, the file structure will still be kept when copying resources to their destination. So when the resources are added in Xcode, they need to be added as yellow folders (which is usually the default option). If you choose not to flatten the paths you should add your resources as blue folders in Xcode.