05/Jul/10
- Read two amazingly clear tutorials, which may help every Cocos beginner: How to make a simple iPhone game with Cocos2D and Intro to Box2D with Cocos2D Tutorial: Bouncing Balls. Also check others tutorials from him.
- PVR – PowerVR graphic file – smaller than PNG and others, but have to be squared and sized as power of two.
- Objective-C: You can’t remove an object from an array while you are iterating through it.
- Will use FSM for enemies in my yet-to-be-announced game.
- XML parsers for iPhone.
- Efficient random for iPhone (source):
int number = (arc4random() % max) + min;
A* Search algorithm
-
A* search algorithm used for path finding and even checking tiles on tiled maps (example: Puyo game from iPhone Games Projects bonus chapter: http://apress.com/book/downloadfile/4448 – it uses A* to iterate through the blocks checking for 4 of the same color).
- Each grid block is a node.
- Nodes that have been examined when looking for a path: closed list.
- Nodes yet to be examined: open list.
- Starting node to goal node: moves list.
- Example A* Cocos2D and objective-C code (haven’t tested it tho): http://www.cocos2d-iphone.org/forum/topic/6828#post-41478
Cocos2D
- Transform anchor: coordinates used by Cocos to rotate objects on screen. For sprites, the default is the middle of the image.
- ccp(x, y) is Cocos shortcut macro to CGPointMake.
- Scene: a container of all the stuff you’re going to show on screen in a specific part of your game: Main menu, level, help, highscores (each one is a different scene).
- Hierarchy: Scene : Layer : Node : Node.
- A node can be a sprite, label, etc. Note that a sprite may have a child sprite.
- Add nodes to a layer with addChild method.
- Scenes have depth ordering (z): lower Z objects appear behind higher Z ones (er, ok, very old thing, we learn this when we are 8? but always worth noting :) ).
- Cocos auto dealloc all children.
- Run an action:
[node (sprite, etc) runAction:[CCSequence (or CCSpawn) actions:action1, action2, CCCallFunN (callback on the object when the actions are performed)]].
- How to load a sprite:
CCSprite *sprite = [CCSprite spriteWithFile:@"image.png" rect:CGRectMake(0, 0, w, h)]; sprite.position = ccp(x, y);
Coordinates
- UIKit: 0,0 from top left.
- Cocos2d: 0,0 from bottom left.
- Sprites: 0,0 from bottom left.
Box2D
- Box units are in “meters” ranging from 0.1 to 10. Use macro: PTM_RATIO 32.0 to convert pixels to meters, example:
b2Vec2(winSize.width/PTM_RATIO, 0);
- World object: manages all the objects and the physics simulation.
- Bodies: movable and static.
Body
- Definition (b2BodyDef): The body definition holds the data needed to create and initialize a body – position, velocity, type.
- Object (b2Body): “body factory”: myWorld->CreateBody(&bodyDef);
- Shape (b2PolygonShape, b2CircleShape, etc): geometry you wish to simulate.
- Fixture definition (b2FixtureDef): binds a shape to a bodys. Shapes don’t know about bodies, that’s why we need fixtures. Defines:
4.1) Density (more dense, more mass = harder to move).
4.2) Friction (0…1 – how hard for objects to slide against each other).
4.3) Restitution (how “bouncy” is an object. 0 = no bounce, 1 = perfectly elastic). - Fixture object (b2Fixture): b2Fixture* myFixture = myBody->CreateFixture(&fixtureDef);. A body may have any number of fixtures.
Cocos2d Image credits: Official wiki







We are Alfred (programming & art) and Débora (ideas) and we are passionate about games. Karnak Games is our indie iPhone and iPad games development studio. 





GamingHorror
/ July 6, 2010“Objective-C: You can’t remove an object from an array while you are iterating through it.”
Yes, you can. If you don’t use fast enumerator but a regular for loop iterated in reverse:
for (int i = [array count] – 1; i >= 0; i–) { //remove object at index i or higher is valid }
In that case you *can* remove the current object (the one at index i) because it doesn’t change the order of objects which are still left to iterate over.
Alfred R. Baudisch
/ July 6, 2010Now everything makes more sense! I was somewhat confused, since C/C++ allows it. I’ll note that on the next post.
Thanks for the tip!
Maciej Siwc
/ October 19, 2010Even better, you can do this which is a lot cleaner, at least in my opinion:
for (id *obj in [objArray reverseObjectEnumerator]) {
[objArray removeObject:obj];
}