Tutorial: cocos2d Xcode Project: Getting our Project Template to build minimal cocos2d code Comments Feed" href="http://www.learn-cocos2d.com/knowledge-base/tutorial-professional-cocos2d-xcode-project-template/feed/"/>

Tutorial: cocos2d Xcode Project: Getting our Project Template to build minimal cocos2d code

Search my cocos2d for iPhone FAQs & Tutorials

Please note that the blog search in the upper right corner doesn’t search my FAQs and Tutorials.
 

  • Note: please do not share direct download links to PDF files, the download links expire after a couple minutes!

We still only have an OpenGL ES application in our project. In the following steps you'll learn how to change this into a cocos2d Project Template much like the "HelloWorld" application. Finally we'll clean up the remaining files from the Open GL ES application.

Download Lesson PDF

Create a temporary Project from cocos2d Project Template

Create a cocos2d Project from the template above (no physics engine) and name it any way you want.

We need this cocos2d Project Template so we can copy some files from it. You can later remove that project.

Recreate the cocos2d AppDelegate

The first thing we're going to do is to recreate the AppDelegate code. I know this explanation isn't going to be very visual and i can't really help with images here. What we want to do in this step is to copy all the content of the xxxAppDelegate files from the newly created cocos2d Project to our project's xxxAppDelegate files, but we want to keep our xxxAppDelegate's class name so we'll have to rename the class afterwards.

Open our project's xxxAppDelegate.h file and remember the class name or copy it to a temporary place, for example a text editor.

In the cocos2d Project you've just created, open the xxxAppDelegate.h file, select all of the content in the file (Command+A) and copy it over to our xxxAppDelegate.h file, overwriting everything that was in there before. Do the same with the xxxAppDelegate.m file. When you're done, the xxxAppDelegate files in our Project should be exactly the same as in the cocos2d HelloWorld project.

Alternatively you could also delete the AppDelegate files in our project, and add them from the cocos2d project while making sure that "Copy items" is used.

Next you should change the AppDelegate class names in our xxxAppDelegate files. Rename the class name and the #import "xxxAppDelegate.h" line in the xxxAppDelegate.m file just like it was before.

Fix main.m to load our AppDelegate

Select main.m from the Groups & Files pane. It doesn't yet reference any AppDelegate. See the next step.

Tell UIApplicationMain what our AppDelegate's name is

Here i've replaced the fourth parameter nil with the name of the AppDelegate in my project. Note that the AppDelegate's name will be different in your project. Make sure you use your AppDelegate's name here!

Make sure you get the class name correct and don't append .h or .m otherwise the app will not start up and you'll be left with a blank, black screen when you start the app later on. If this happens to you, and any breakpoint set in applicationDidFinishLaunching: seems to be ignored, then chances are your AppDelegate is named incorrectly in the UIApplicationMain method call here.

Add the HelloWorldScene class

Right or control click the Classes group and choose to Add Existing Files.

Locate the HelloWorldScene.h and .m files of the cocos2d project.

Copy the HelloWorldScene.* files

In this dialog make sure that "Copy items ..." is checked, then click Add. You will now have the HelloWorldScene class files in your project.

Delete some unneeded files

Delete the files and groups highlighted in the image from our project. Make sure you remove the groups, this will also remove the contents of the group. That is what we want.

Move these files to the Trash

When you're asked to "Delete References" respond by clicking the "Move to Trash" button. We really don't need these files anymore.

Add required Frameworks

We need to add some specific Frameworks to our project needed by cocos2d. Right or control click the Frameworks group and select Add --> Existing Frameworks.

Select Frameworks from the List

Our project already contains references to the frameworks for QuartzCore, OpenGLES, UIKit and Foundation. In addition we need to add the following frameworks:

AudioToolbox.framework
AVFoundation.framework
CoreGraphics.framework
OpenAL.framework
libz.dylib

The dialog shown in the image allows you to select multiple items at once.

Important: to locate the libz.dylib you'll have to scroll down to the very bottom of this list. Don't forget to add this file too. And make sure you don't accidentally select libz.1.dylib.

List of Frameworks

For reference, here are the frameworks we need in our project.

Add missing resources

We now add the missing resources from the cocos2d HelloWorld project. We'll do that by adding the "Resources" folder from the cocos2d project. Do the usual. Right or command click the Resources group and select Add --> Existing Files despite the fact that we actually want to add a whole folder.

Select the Resources Folder

Browse for the cocos2d HelloWorld project select the Resources folder. This time we'll add the Resources folder itself because it contains all the files we need, and our project doesn't have a Resources folder yet.

Note: if it says "can't add group" then check your project folder if there is a subfolder named "Resources". If so, delete it and try this step again.

Make sure to "copy items"

Again, make sure that the "Copy items" checkbox is checked.

Verify Resource group exists

Verify that the Resources group was added and contains these files.

Remove Info.plist from build

We don't want or need our Info.plist file in the app. Select Info.plist and uncheck the checkbox.

Set our Target to use Info.plist

Right or control click on our Target and choose "Get Info". Switch to the Build tab. Make sure that "Configuration" is set to "All Configurations" and "Show" to "All Settings". Enter "plist" in the search box. You should see something similar to the image above.

Modify the Setting "Info.plist File" so that it reads "Resources/Info.plist". Make sure you enter it as shown including the path to the Resources folder and upper/lowercase must be correct. Meaning: don't name it "info.plist" or "INFO.plist".

Close the Info dialog.

Add cocos2d.h to the Prefix header

Select the xxxx_Prefix.pch file and add the line to the #ifdef __OBJC__ section:

#import "cocos2d.h"

This is not strictly necessary, and the cocos2d project doesn't have that either. But it will build our code a little faster and you never have to import the cocos2d.h header anywhere anymore. You can read up on Wikipedia about Prefix Headers if you want to know more.

...

I know what you're thinking now, why not add ALL headers to the Prefix.pch file? The general advice is not to add headers of your own project to the Prefix file because if you do that you'll actually see an increase in compilation time! Because any change you make to one of your header files which is included in the Prefix.pch will cause your whole project's source code to recompile! So don't add your own header files to the Prefix.pch. However, you are encouraged to add any 3rd party headers to it, that's what the Prefix.pch is for.

Build & Run

The code should now build successfully and run in Simulator. You'll see something similar as above. Well done!

You can now delete the temporary cocos2d HelloWorld project we created. We don't need it anymore.

Comments (1)

  1. Matthew

    Like a Boss! This was fantastic. Well done and well illustrated. If all tutorials were this visual we would have a lot less problems in the forums. Thanks so much Steffen - I used the tutorial to set up my system tonight.

    Peace

Tutorial Poll

What cocos2d or programming How-To/Tutorial would you like me to create? Choose up to three:

View Results

Loading ... Loading ...

170 Responses to “Tutorial: cocos2d Xcode Project: Getting our Project Template to build minimal cocos2d code”

  1. Carl says:

    I get “cocos2d.h:No such file or directory” when trying to #import “cocos2d.h”

    what am I missing?

  2. vik says:

    This tutorial is toooo long.

    Is there a short version ? I spent two hours on this and as I expected nothing works.
    For begininers its not going to work

  3. Norm says:

    Great info! Thanks! However, the script is building the .ipa file before it does the codesign step. Have things changed in Xcode? How do I get the script to run after the codesign step?

  4. zutroi says:

    I tried twice to follow your tutorial step by step using cocos2d 0.99.5-rc1, Xcode 3.2.5 and iOS 4.2.1, but I keep having problems when building and running on device.
    The weird thing is I have no problem at all running the template on simulator!
    To be more clear:
    I’m trying to build and run following the final step of “Getting our Project Template to build minimal cocos2d code” section.
    Running on simulator works perfectly, running on device (iphone4) crashes after splash screen with this message on console:
    “Program received signal: “EXC_BAD_ACCESS”.
    warning: Unable to read symbols for /Developer/Platforms/iPhoneOS.platform/DeviceSupport/4.2.1 (8C148)/Symbols/Developer/usr/lib/libXcodeDebuggerSupport.dylib (file not found).
    Previous frame inner to this frame (gdb could not unwind past this frame)”
    By stepping with debugger I found the app crashes on line 98 of AppDelegate.m:
    “// make the View Controller a child of the main window
    [window addSubview: viewController.view];”

    I see there are little differences from your (great) tutorial and Xcode/cocos2d new templates, mainly for the use of RootViewController, may this crash be related? (I already added RootViewController.* to my template)
    Has anyone found a fix to this problem?
    Please please please help me, this thing is driving me crazy =/
    Thanks

    • It might be related. Have you tried the version on github? https://github.com/GamingHorror/cocos2d-project
      That version is tested with 0.99.5

      But I recently started getting those “could not unwind” errors … I was able to fix them by changing the compiler in all referenced projects back to GCC.

      • zutroi says:

        Bingo!
        Thanks a lot, you point me to a fix.
        I had already tried with your latest github version, without luck.
        I followed your suggestion, changing the compiler to GCC 4.2 on all projects (both cocos2d-project and cocos2d-ios) and it worked!
        I suppose the problem was in compiler setting for cocos2d-ios, it was set to LLVM compiler 1.6.
        If I set compiler to different version for the two projects, I always get that crash, when using same gcc version, everything is ok.
        I tried also with LLVM GCC4.2 on both projects and it works too.

        Probably I never met this problem since using cocos2d built-in templates everything get compiled using gcc without further setting.
        Thanks a lot for your tutorials… and for fast replying too ;)

  5. Paris says:

    Hi Steffen

    To upgrade a project created with your template from e.g. 0.99.5 rc1 to the final 0.99.5 I just need to copy over the cocos2d-IPhone folder?

    I would assume yes but thought of asking since it does not clearly covered in the main points/links at the top of this tutorial

  6. Mehdi says:

    I receive a lot of EAGLView may not respond to …xxx warnings, and I believe these are the undeclared selectors you are mentioning in your comment. How can I fix it? Only with turning off the treat warning as error option or also by changing the code?

    I didn’t understand where to implement the code from your comment on this tutorial.

  7. rastermax says:

    Hi
    1st: GREAT job on this PDF. Realllly appreciated.

    I just did the first part up to Helloworld with the 99.5 version. (up to page 43 of pdf)

    You might want to update a bit, the part where we get the AppDelegate from Helloworld sample : since in 99.5 Cocos changed the samples so that for exemple main.m does not exists main() is in the delegate.m file.

    Continue the great work.

Leave a Reply