Learning Cocos2D

Rod Strougo, Ray Wenderlich

Mentioned 7

Provides information on the installation and configuration of Cocos2D to build complete 2D games for the iPhone and iPad.

More on Amazon.com

Mentioned in questions and answers.

I'm making a simple cocos2d adventure game, but have no clue how to implement any sort of levels. I've searched for tutorials, but can't find any.

Is there anything I can use to figure out levels in cocos2D?


You mentioned not being about to find any tutorials. I agree that finding free online tutorials for cocos2d can be challenging. I ran into the same problem when I started learning it. I recommend grabbing a book on cocos2d such as Learning cocos2d. There is so much to the API that you will have a very hard time creating even a rudimentary game without any tutorials or guidance, unless you have a lot of prior programming experience.

My question is How to move a ball in cocos 2d using Space manager.but when the ball move the screen will also move like angry bird.

Thanks In Advance.

"Learning Cocos2D" by Strougo and Wenderlich is pretty good, although there are some omissions. For example it doesn't discuss how to save state, although people might be expected to look elsewhere for that because it's standard iOS. http://www.amazon.co.uk/Learning-Cocos2D-Hands-Building-Addison-Wesley/dp/0321735625

On the other hand, Ray Wenderlich's website is fantastic and discusses a lot of stuff that doesn't make it into his book, including saving state. http://www.raywenderlich.com/

Also check out the official Cocos2D programming guide, which is probably how many of us got going. http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:index

If you don't actually know Objective-C yet, or any other language, Kochan's book is good: http://www.amazon.co.uk/Programming-Objective-Stephen-G-Kochan/dp/0672325861

If you already know something like Java, C++, or C# then O'Reilly's Objective-C Pocket Reference is like a mini-conversion course. It's pretty old but everything in there is stuff you'll still need. http://www.amazon.co.uk/Objective-C-Pocket-Reference-Andrew-Duncan/dp/0596004230

Say there was a game, where you stay on a single screen (unless you pass the level), and you are a man walking around in space.

It seems like a tile based game to me, the only catch is that when you move, it has some physics at play, meaning the more you press up, the faster you move and the longer it takes to slow down.

And you also keep moving even if you stop pressing the arrow keys as you have gained momentum.


how would I handle the movement of the man based on momentum etc?

If you decide to use Cocos2d, start with this

alt text

Learn iPhone and iPad cocos2d Game Development

It has an example in the chapter "Your First Game" which covers Velocity calculations and controlling a players movement using these calculations.

Also as bbum mentioned Ray Wenderlich has some great tutorials and is really educating developers on some interesting topics.

He also has co-authored a book which is available for pre-order. alt text

Learning Cocos2D: A Hands-On Guide to Building iOS Games with Cocos2D, Box2D, and Chipmunk

I'm French so excuse my poor English.

I'd like to know is if Tiny Wings, Angry Birds, and other great applications like that use cocos2D, for some or all of their game. I want to use the particle designer and we must use it with cocos2d, but I don't like the fact that the image of cocos2d appears on my application on the App Store.

Tiny Wings, Angry Birds, Mega Jump, Trainyard... to name a few, use cocos2d, box2d, or both.

Source: A description of a cocos2d book that I'm waiting to be released.

Also... you can change the default loading image of the application... easily.

-- EDIT --

To remove the frame rate from your application, remove the following line of code from your app delegate file:

[[Director sharedDirector] setDisplayFPS:YES];

Or, you can leave the line there but set it to NO.

I'm trying to write my little app and experiencing some memory management problems. At first, I have Game singleton object with property:

@interface Game : NSObject

@property (nonatomic, retain) MapBuildingsLayer *mapBuildingsLayer;

+(Game *) game;
-(BOOL) addObject:(NSString *) objName At:(CGPoint) pt;


where MapBuildingsLayer is just cocos2d CCLayer instance

@implementation Game
@synthesize mapBuildingsLayer = _mapBuildingsLayer;

static Game *instance = nil;

+ (Game *)game {
    @synchronized(self) {
        if (instance == nil) {
            instance = [[Game alloc] init];
    return instance;

-(BOOL) addObject:(NSString *)objName At:(CGPoint)pt
    if([objName isEqualToString:OBJ_TYPE_PIT])
        if([[Game game].mapBuildingsLayer addPitAt:pt]) //app crashes here
            [self toggleConstructionMode];
            return YES;
    return NO;


In MapBuildingsLayer.m's init method I use Game's mapBuildingsLayer property to store a reference to this CCLayer instance in my singleton (for future use in other methods):

@implementation MapBuildingsLayer

-(id) init
    if( (self=[super init])) {
        [Game game].mapBuildingsLayer = self;
    return self;

When I call Game's addObject:objName At: method, my app crashes with EXC_BAD_ACCESS. How I must declare property in Game singleton to use it from other places in my app's lifetime?

You typically don't use the singleton in the class itself, try changing

        if([[Game game].mapBuildingsLayer addPitAt:pt]) //app crashes here


        if([self.mapBuildingsLayer addPitAt:pt]) //app crashes here

You should be using [Game game] external to the class to get into the singleton instance of your class and call its methods, but internal to the class you would just refer to it as self like normal.

Typically if you're going to use a game singleton this isn't how you'd use it. Try thinking of it like this with a giant state machine, Create a CCScene subclass which will initialize all of your respective CCLayer subclasses and control them. Then from the statemachine you can load the appropriate initializing its scene and that will create everything under it.

In your applicationDidFinishLaunching method you simply have your singleton object load your first scene. I'd really recommend checking out the Learning Cocos2d Book as it covers this singleton state engine very well and I think would clear up all your questions.

Bottom line is have the state engine load the scene which loads the layers.

I wanted to learn some graphics and thereby gaming coding in iPhone. I have experience in Nav/Tab/View based applications but never worked on OpenGL or Quartz.

Could anyone guide my way to this new exciting journey of graphics. Please suggest tutorials, session videos, example videos... basically anything that could help me understanding the related concepts.

Thanks in advance!

PS: I have a developer account.

I would suggest that you also consider using Cocos2d, Box2d or something similar. Here is a good book.


Ray Wenderlich also has a game programming starter kit on their website which you could consider buying - http://www.raywenderlich.com/store/space-game-starter-kit

Here are a couple good and free resources:

Those are couple good books and videos. Also make sure to check out Ray Wenderlich's site; it contain's many good, complete tutorials for beginners. Hope this helps!

Like, should I read this book: iOS Programming before starting with this book: Learning Cocos2D, or could I just start with the second book right away? I'm going to make iOS games, not utilities etc. I know Objective-C.

You don't need to. Cocos2D only requires you to create a Cocos view and manages all the UIKit stuff behind the scenes, however you will miss on some interesting Apple-provided frameworks.

If you want to use geolocation, send emails, open SMS, add advertisement, play music from the user's Library, synch into Game Center, allow in-app purchases, you will need UIKit. My advice is therefore: go into Cocos2D first to have something fun rolling, and then learn what you need to make your game better/more profitable.