Getting Started with Cocos2d

Cocos2D for iPhone is a great open source framework that makes it easy to make some very impressive 2d games with much less effort than having to know OpenGL directly or rely on a third party system that might have performance issues or be unnecessarily bloated.   From the official cocos2d for iphone website:

Cocos2D for iPhone is a framework for building 2D games, demos, and other graphical/interactive applications. It is based on the cocos2d design: it uses the same concepts, but instead of using python it uses Objective-C.”

Many of the most complex features are done for you, but like any open source software you can make changes as required.  I however, doubt you will need to do many modifications on your own given that it’s very easy to subclass and handle drawing yourself as you become more experienced.  Using Cocos2d is really one of the best ways to get your feet wet with OpenGL programming and learning Objective-C. HOWEVER, it is a programming language and that means you will need to understand the fundamental concepts of programming.

Cocos2d for iPhone is:

  • Easy to use: it uses a familiar API, and comes with lots of examples
  • Fast: it uses the OpenGL ES best practices and optimized data structures
  • Flexible: it is easy to extend, easy to integrate with 3rd party libraries
  • Free: is open source, compatible both with closed and open source games
  • Community supported: cocos2d has an active, big and friendly community (forum, IRC)
  • AppStore approved: More than 1000 AppStore games already use it, including many best seller games.

cocos2d for iPhone supports: iPod Touch, iPhone and iPad

Download the latest build at:

As of the writing of this – The latest stable build was 0.99.4 and the package runs about 30 megs with all sample code and libraries included

Because no one seems to read the readme files anymore until they get into trouble, I am listing some of the things to note that might otherwise be overlooked.

Build Requirements
——————
Mac OS X 10.5.3, Xcode 3.1, iPhone OS 2.0, Beta 8 release or later
Runtime Requirements
——————–
Mac OS X 10.5.3, iPhone OS 2.0, Beta 8 release or later
Files and Folders
—————–
* cocos2d/   <— core files and folders of cocos2d
* cocoslive/ <— high score client library
* CocosDenshion/  <— sound engine library
* tests/  <— tests, demo and template examples
* experimental/ <— experimental stuff
* external/ <— 3rd party libraries used by cocos2d or any other project.
* Chipmunk/:  physics library
* Box2d/: physics library
* TouchJSON/: JSON parser
* Tremor/: OGG audio decoder
Setup Script:
Ok now to the good stuff – one of the real nice items that has been included in the package is an install script that will populate your “user templates” in xcode with a few predefined ones for cocos2d.  This will save tons of time in the initial setup of cocos2d.
Run the ‘install-templates.sh’ script as root:
$ cd cocos2d-iphone
$ sudo ./install-templates.sh
OR with `-u` to install the templates in your user directory:
$ cd cocos2d-iphone
$ ./install-templates.sh -u
And then open XCode -> New Project -> User Templates -> cocos2d
That’s it for now – If you have any problems with the initial setup of cocos2d, either post it in our forums or use the “cocos2d for iphone” reference below to find a solution.  Hopefully that’s enough to get you started for now and we can begin diving into the code more directly!
References Link!
Main Cocos2d Websites:

Site with a few open source games (and the rest are some great open source projects) for the iphone
http://maniacdev.com/2010/06/35-open-source-iphone-app-store-apps-updated-with-10-new-apps/

Saving State in Cocos2d:
http://www.cocos2d-iphone.org/forum/topic/737#post-8224

Box2d: extending b2ContactFilter and b2ContactListener:
http://blog.xyris.ca/?tag=box2d

Box2d Reference and Tutorial (C++):
http://www.box2d.org/manual.html#_Toc258082967

Cocos2d iPhone API Reference:
http://www.cocos2d-iphone.org/api-ref/latest-stable/annotated.html

Cocos2d iPhone Google Code Page:
http://code.google.com/p/cocos2d-iphone/

Chipmunk API Reference:
http://code.google.com/p/chipmunk-physics/wiki/cpShape

Using Chipmink ONLY for collision detection:
http://www.cocos2d-iphone.org/forum/topic/577

Chipmunk 5.0 New Collision Detection Function:
http://www.cocos2d-iphone.org/forum/topic/3996

Vector Dot Products:
http://www.algebralab.org/lessons/lesson.aspx?file=Trigonometry_TrigVectorDotProd.xml

Easing Actions in and out:
http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:actions_ease

Using CCCallFunc with parameters:
http://www.cocos2d-iphone.org/forum/topic/721

Cocos2d iPhone Best Practices:
http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:best_practices

Great tutorials and lists:
http://kwigbo.com/
http://www.bit-101.com/blog/?p=2138
http://iphonedev.net/
http://www.alexcurylo.com/blog/2009/05/16/resources-cocos2d/
http://morethanmachine.com/macdev/?tag=cocos2d
http://silentmac.com/?tag=cocos
http://vinc456.wikidot.com/cocos2d

Touch Event Handler:
http://lethain.com/entry/2008/oct/20/touch-detection-in-cocos2d-iphone/

http://permadi.com/blog/2009/06/cocos2d-adding-handler-to-touch-events/

Overview of Cocos2d (a little old but still good):
http://lethain.com/entry/2008/oct/03/notes-on-cocos2d-iphone-development/

Moving a Sprite:
http://iphonedev.net/2009/05/10/cocos2d-example-move-sprite/

Coloring Sprites:
http://lukehatcher.com/post/449164972/coloring-sprites-with-cocos2d-iphone

Objective-C Memory Management:
http://www.bit-101.com/blog/?p=2233

Great Beginner Forum Post:
http://www.cocos2d-iphone.org/forum/topic/737

iPhone Custom Made Sound Manager:
http://www.71squared.com/2010/01/latest-sound-manager-class/

How to fix iPhone Signing Errors:
http://www.codza.com/how-to-fix-iphone-code-signing-errors

Game Examples:
http://codaset.com/lhunath/deblock
http://wiki.github.com/lhunath/Gorillas/
http://code.google.com/p/grabbed/
http://kwigbo.com/post/335053103/pusher-source-code
http://ortatherox.com/thrown/
http://www.ortatherox.com/site/
http://github.com/haqu/tweejump

Sound Effect Libraries:
http://www.grsites.com/archive/sounds/

Objective-C Docs:
http://www.cocoadevcentral.com/

Optimizing Texture Loading in Cocos2d:
http://allseeing-i.com/Performance-tips-for-Cocos2d-for-iPhone

Share this:
Share this page via Email Share this page via Stumble Upon Share this page via Digg this Share this page via Facebook Share this page via Twitter

1 Response to “Getting Started with Cocos2d”


Leave a Reply

Notify me of followup comments via e-mail. You can also subscribe without commenting.

Tip: When posting code, put the following around your code for a better view:
<pre class="brush:c++">your code here</pre>




Switch to our mobile site