Learning cocos2d with samples

Arguably the most unknown cocos2d feature is the tests/samples.
cocos2d contains almost a test for each feature that it has. These tests are grouped by component. eg:

  • Sprite Test: It tests the CCSprite and CCSpriteSheet functionality like anchorPoint, visibility, parenting, reordering, honor Transform, etc…
  • Action Test: It tests the CCMove, CCRotate, CCTint, CCCamera, CCSequence, CCSpawn, etc..
  • Menu Test: It tests all kind of menus
  • CocosDenshion: It tests the Sound Engine
  • Chipmunk Accel Touch Test: It tests integration with Chipmunk physics engine using accelerometer and multi touches.
  • Particle Test: It tests all kind of particles (quad particles, point particles, sun, meteor, galaxy, smoke, fire, etc…)
  • etc…

Although these tests were coded for testing purposes they can also be used as examples. Not only they use the API in the recommended way, but some of them, also contain useful tips in the commentaries.

In order to run the tests, you should do:

  1. Open the cocos2d Xcode project
  2. Select the test target: Xcode -> Project -> Set Active Target -> eg: TransitionTest
  3. Make sure that the running executable is correct: Xcode -> Project -> Set Active Executable -> eg: TransitionTest
  4. Build and run: Xcode -> Build and run

Another quick way of doing it by customizing the toolbar:

  1. Xcode -> Right click on the toolbar -> Customize Toolbar
  2. Drag & drop the Active Target button to the toolbar
  3. Drag & drop the Active Executable button to the toolbar

The source code of the samples are in the tests subdirectory.

So, let’s say you want to learn how to use Tiled maps. What you should do is:

  1. Select the TileMapTest target
  2. Make sure that the TileMapTest executable is selected
  3. Build & Run.
  4. Try all the subtests. The TileMapTest has more than 10 subtests. Eg: Hexagonal test, Isometric test, Orthogonal tests, read-write tile tests, etc…
  5. See the TileMapTest source code: tests/TileMapTest.m
  6. Modify the source code and go to step 2. :-)

9 Responses to “Learning cocos2d with samples”


  • Everytime I try this I just get “Code Sign error: The identity ‘iPhone Developer’ doesn’t match any valid certificate/private key pair in the default keychain”, what am I doing wrong?

  • Try getting them to work in the Simulator first, getting stuff working on the iPhone itself is a whole topic in itself. The place to look is the developer connection on apple unfortunately I can’t paste the link at the moment as its being updated.

  • No that’s my point, I don’t even have an ADC account yet to get it working on the device. I downloaded cocos, ran the project in question, did what this tutorial said, and I get that error. I assume it’s because that project was created by someone with an ADC account and I don’t have one or something? I did actually turn off code signing in the project settings but it didn’t seem to matter so I guess that’s not it.

  • Nevermind, I set the executable incorrectly. It’s nice to have these instructions but they don’t go into enough detail for the dummies like me!

  • Every time I “Build & Run” I get the error “No launchable executable present at path.”

    What I did wrong? :/

  • Im trying to make a hexagonal map and was wondering what program you used to create this map. Thanks for the info. The examples are awesome!

  • Nevermind, I just found out I have to use the java version for hexagonal maps.

  • I had just posted a question about this on the forum before I found the comments page on this blog entry. So I will post it here, since this is where I probably should have posted it in the first place.

    As a new user to cocos2d, the instructions given are not quite clear to me.

    1. Open the cocos2d Xcode project ( I assume this means to create a NEW Xcode project with the regular cocos2D template).

    2. Select the test target: Xcode -> Project -> Set Active Target -> eg: TransitionTest (When I try to do this it gives me only two choices, 1 – The name of the new project I created (ex. “Testing1″) or 2 – “cocos2D libraries”

    3. Make sure that the running executable is correct: Xcode -> Project -> Set Active Executable -> eg: TransitionTest (When I try to set the active executable, the only option is the name of the new project I created (ex. “Testing1″)

    4. Build and run: Xcode -> Build and run (This I understand, if I could get step 2 and 3 to work)

    On step 2, if I select “cocos2D libraries” I still can only select the name of my new project from step 3.

    Following the given instructions above I am not able to select any of the tests for Active Target or Active Executable. Could someone please explain what I need to do or what I am missing so I can build and run these tests?

  • @MisterX You don’t have to create a new XCode Project. The cocos2d bundle you’ve downloaded contains the mentioned cocos2d Xcode project.
    Extract the .tar.gz file -> open the extracted folder -> double-click on cocos2d-iphone.xcodeproj in order to open the cocos2d project.

Leave a Reply