The latest versions of Cocos2D for iPhone include Xcode project templates to get you running quickly. You can install them by executing the install_template.sh script, which places said templates in the /Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Project Templates/Application folder.
Maybe these templates don't quite cover your needs, so we will learn how to create our own. You can also take a look at my Xcode project template for Cocos2D v0.8.2, in case it suits you.
Create the template
First, download the latest version of Cocos2D and uncompress it to /Developer/Library. Now launch Xcode, open its Preferences window, and create a new source entry that points to the folder containing Cocos2D:

If you go to /Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Project Templates/Application, you will see all the default iPhone project templates. The most basic template is the one called Window-based Application. If you open its folder, you will see it contains two others: Window-based Application and Window-based Core Data Application. We are interested in the first one. Copy it, rename it to something like Cocos2D Application, and place it in /Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Project Templates/Application. You should have something like this:


Open your Cocos2D Application project in Xcode by double clicking the file named ___PROJECTNAME___.xcodeproj. The project will look like this:

Delete the nib file MainWindow.xib, as we don't need it for our project. Also delete the entry Main nib file base name from our plist file ___PROJECTNAMEASIDENTIFIER___-Info.plist. While you are at it, you can add the entries Status bar is initially hidden and Initial interface orientation, in case you want to hide the status bar and change the interface orientation to landscape, respectively:

Right click on the Frameworks group and select Add > Existing Frameworks.... We need at least OpenGLES.framework and QuartzCore.framework, but if you are going to need any others, include them too.

Now let's add some Cocos2D to our template. There are many ways to do this, but I think the best one is to add a reference to its Xcode project, and include the libraries we require. To keep things organized, create a new group named Support, and inside it another one named Cocos2D. Right click on this last group and select Add > Existing Files.... Navigate to /Developer/Library, open the Cocos2D folder, and select the file cocos2d-iphone.xcodeproj.
In the dialog that appears, uncheck Copy items into destination group's folder, and select as reference type Relative to Cocos2D (or whatever you named your source entry):

If you click on the newly added cocos2d-iphone.xcodeproj, Xcode will list all the files contained in the Cocos2D project. Scroll down until you see the static library libcocos2d.a, and tick its checkbox to link your application against it:

Now double click on the target ___PROJECTNAME___, select the General tab, and add Cocos2D as a dependency, so that it is built before our project:

Switch to the Build tab, find the entry User Header Search Paths, and add $(COCOS2D_SRC):


Finally, modify main.m to specify our application delegate:
int main(int argc, char* argv[])
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, @"___PROJECTNAMEASIDENTIFIER___AppDelegate");
[pool release];
return retVal;
}
All that's left to do is write your template code in ___PROJECTNAMEASIDENTIFIER___AppDelegate.h and ___PROJECTNAMEASIDENTIFIER___AppDelegate.m.
Change the description and icon
If you create a new project in Xcode, you will see our new template:

However, the description and icon are the same as those of the Window-based Application (obviously). Let's solve that. From the Finder, right click on the file ___PROJECTNAME___.xcodeproj, and select Show Package Contents:

Edit TemplateInfo.plist to add your description:
<plist version="1.0">
<dict>
<key>Description</key>
<string>This template provides a starting point for a Cocos2D application.</string>
</dict>
</plist>
To change the icon, we just have to replace the file TemplateIcon.icns with our own. To create a icns file, launch the app Icon Composer found in /Developer/Applications/Utilities:

Just drag and drop the image you want to use for your icon in the different boxes. I used an image that comes with Cocos2D, found in the Resources folder:

Save your creation with the name TemplateIcon.icns, and replace the original. Try to create a new project from Xcode, and this time it will show up with our icon and description:

That's it, we are done! I hope to see your amazing templates uploaded to GitHub.
Comments