Learning cocos2d with samples
Published by cocos2d. 9 Comments
on March 3, 2010 inArguably 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
andCCSpriteSheet
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:
- Open the cocos2d Xcode project
- Select the test target: Xcode -> Project -> Set Active Target -> eg: TransitionTest
- Make sure that the running executable is correct: Xcode -> Project -> Set Active Executable -> eg: TransitionTest
- Build and run: Xcode -> Build and run
Another quick way of doing it by customizing the toolbar:
- Xcode -> Right click on the toolbar -> Customize Toolbar
- Drag & drop the Active Target button to the toolbar
- 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:
- Select the TileMapTest target
- Make sure that the TileMapTest executable is selected
- Build & Run.
- Try all the subtests. The TileMapTest has more than 10 subtests. Eg: Hexagonal test, Isometric test, Orthogonal tests, read-write tile tests, etc…
- See the TileMapTest source code: tests/TileMapTest.m
- Modify the source code and go to step 2.