Learn & Master

Cocos2D Game Development

            "Crack that Coconut!"

Learn Game Development with Cocos2D

The Cocos2D for iPhone game engine is the premier 2D game engine for iOS and Mac OS X 2D game developers. It’s no secret that sometimes it can be a tough nut to crack. I’m Steffen, book author, blogger, game developer and nutcracker.

What is it that you want to know?

Learn Cocos2D Game Development

Connect with me!

Join Cocos2D Central and start a discussion. That’s the best way to reach me. I also answer questions on StackOverflow.com.

You should also subscribe to my newsletter to receive updates and never miss anything important.


You should definitely follow me on Twitter if you use Twitter. If you don’t, now would be a good time to start!

225 Responses to “Learn Game Development with Cocos2D”

  1. Qpido says:

    Great book so far, enjoyable read. I have the 2nd version, are you going to be releasing a third on soon with updates like ARC and cocos2d-iphone 2.0beta?

    Thanks!

    Q~

    • Yes, I’m currently working on the 3rd edition. The 3rd edition discusses cocos2d 2.0, uses ARC for all projects, and is compatible with Kobold2D. It’ll be out late July or August.

      • Peyo says:

        I’m about to start “training myself” with Cocos2D. I opted for a Kobold2D set-up for obvious reasons.

        Should I wait until Kobold2D v2 is out which can’t be too far away ? And for issue 3 of your book by the way to get the right instructions ?

        Or will it be easy to upgrade when it’s out ?

        Many thanks in advance

        • Kobold2D v2 should be out soon. The 3rd edition is planned for July but could be August.
          Upgrading from cocos2d v1.x to v2.x can be a simple or complex task. It depends on how big the project is and how much it uses parts of the API that has changed in v2.

          • Peyo says:

            Thanks for your reply Steffen. Until August I reckon I only have time for learning, so I won’t have projects to upgrade, just my brain/understanding will need to adjust. So I’ll install Kobold2D v2 and start as soon as it is available, and get the book as soon as it is out (will use the tutorials and forum to start with). Thanks again.

  2. Abhinav says:

    Hi Steffen,

    Just wanted to drop a thank you note for creating GKHelper. Very neat and useful. Thanks!

    Best
    Abhinav

  3. Hey, I’m just starting out with cocos2d and extremely confused/unable to find good resources online. I was thinking about getting your book but with version 3 coming out soon would it be better for me to wait for that? I don’t really know much about developing without ARC which has been the hardest part of learning cocos2d.

    • I believe it’ll be well worth the wait. It should be out in about 4-6 weeks. It’ll be the first cocos2d book to cover cocos2d 2.0 and ARC (and the text & code are compatible with Kobold2D 2.0).

      • Thanks for the response! I think I will wait then. The non-arc stuff is what confuses me the most because I just started learning iOS and arc is the norm for me with a Java background. It’s hard to get started with cocos2d when all the templates they give you are non-arc and I can’t figure out how to load in a view in an arc project. As for Kobold2D I’m really interested in that but is it better to know cocos2d first? I never really found any information on Kobold2D that explained that.

        • Absolutely, Kobold2D is more or less a wrapper around cocos2d. Give or take a few details (startup specifically) you can use the same code from existing cocos2d tutorials.

          Also search my blog for ARC, I’ve got a few tutorials on ARC and how to enable ARC in a regular cocos2d project (also described in the book, just much better explanation thanks to copy editing and tech reviewers).

  4. Sunny says:

    Here’s a blogspot I made, and I will be posting tutorials very frequently. I already have 4 up in about 2 days!

    http://learning-cocos2d.blogspot.com/?view=sidebar

  5. AD says:

    Better documentation on how to integrate UI components like Table Views, Sliders, input gadgets, etc with cocos2d — I still don’t know how to put CCSprites or nodes onto a tableview.

    Thanks

    • You can’t add sprites or any cocos2d node as part of a UITableView. You would have to emulate the UITableView behavior in cocos2d. There’s a class CCTableView that implements some of that behavior, but I don’t know if it’s maintained. The announcement is from 2010: http://www.cocos2d-iphone.org/archives/943

      • CocosKat says:

        Hi Steffen,

        I have a CCSpriteFrameCache that I use for various game graphics. However, I also have a CCLayer where I display various images in a UITableViewCell (which are the same images as in the game). I’d rather not add all these image files to the project so that the UITableViewCell can use them (since they are the same as in the CCSpriteFrameCache).

        Just wanted to double check to see whether it’s still true that you can’t use a sprite from a CCSpriteFrameCache as an image in a UITableViewCell. If not is there any alternative besides adding the image files directly to the project?

        thank you for your guidance

        • That’s still true and will always be true. Cocos2D uses OpenGL. OpenGL content is drawn to a OpenGL view, and other views are either above or below that view. What you’re trying to do is the equivalent of sneaking in another view between, say, a UIButton’s underlying button image and the button text. It just won’t work.

          • CocosKat says:

            Thanks for the reply Steffen. I was able to work around this by doing the following however:

            //Get CCSprite from the CCSpriteFrameCache
            CCSprite *foodItemSprite = [CCSprite spriteWithSpriteFrameName:[aDict objectForKey:@"FoodItemImageName"]];

            //Convert the CCSprite to a UIImage
            UIImage *imageFromSprite = [self renderUIImageFromSprite:foodItemSprite];

            //Put that UIImage into the table cell
            cell.imageView.image = imageFromSprite;

            – (UIImage *) renderUIImageFromSprite:(CCSprite *)sprite {

            int tx = sprite.contentSize.width;
            int ty = sprite.contentSize.height;

            CCRenderTexture *renderer = [CCRenderTexture renderTextureWithWidth:tx height:ty];

            sprite.anchorPoint = CGPointZero;

            [renderer begin];
            [sprite visit];
            [renderer end];

            return [renderer getUIImage];

            }

            This appears to work ok. Do you see any risks to this approach? The UITableView is on a CCLayer that just has a static background; there is no action on this layer; it’s basically just a static information screen.

            thanks for your expert opinion

  6. Long says:

    I have finished your book named Learn iPhone and iPad cocos2d Game Development,and it’s a great book.
    and i want to improve my skill about cocos2d iPhone programming,please tell me something examples about open source game based on cocos2d.
    Thanks!

  7. Rick G says:

    Hi Steffen,

    I have a couple questions for you… first off, would it be hard to take your new book (Learn Cocos2d) and apply most (all) that info to a Mac cocos2d project? Secondly, how hard (or even possible) is it to take a game written for Mac and/or iOS and port it to desktop PC and/or xna type thing for Xbox distribution?

    Thanks… great stuff btw.

    • You can build Mac apps with almost the same code. In Cocos2D you’ll have to setup an extra project, in Kobold2D you already get both iOS and Mac as targets in most template projects. The main API differences are when it comes to input, Kobold2D makes that easier with KKInput.

      Porting a game written in Objective-C with cocos2d-iphone to another platform is hard work. You have to consider two things: the engine, and the programming language. While Cocos2D provides the same concepts and to some extend also the same or very similar API across platforms, only cocos2d-x and cocos2d-iphone are relatively complete. There’s a cocos2d for XNA but I don’t know how mature it is, and I haven’t heard users talking much about it.

      Then there’s the programming language. Obviously you’ll have to port the Objective-C code, in the case of XNA that means C#. While the structure of the program can remain fairly intact because both languages are object-oriented, it’s a ton of tedious, boring work to transcribe each Objective-C class to a corresponding C# class. It’s also a difficult process, where the goal is *not* to convert all the code and then see if it runs. There will be problems and they will be hard to track down. Instead you’ll have to convert the very basics first, starting with an empty screen. Then slowly transcribing more and more classes and frequently checking if they run as intended. There are some tools that help in converting source code between programming languages, but it’s nowhere near flipping a switch. The services that promise to port existing apps easily, they weren’t made for games. They’re slowly catching up, but there will always be a significant performance penalty with those services.

      My recommendation for cross-platform development is to ignore it. Focus on one platform. Finish the game, release it. If you got a hit, you can start thinking about porting the game. If the game was so successful that you became a little famous for it and have a fanbase, you can consider developing your next game cross-platform from the start. But first, you have to have that one hit app on whichever platform before it is sensible wasting any time thinking about cross-platform development. It’s a little different for contract programmers, and small studios with enough finances to begin with, but if in doubt I’d still stick to one platform first.

  8. Aldo Bravo says:

    Good morning everybody

    Firstly congratulations for this interesting blog.
    I´m starting to learn a little bit Cocos 2D and I would like to share with you the information about this excellent course that I found:

    http://agbo.biz/tech/curso-cocos2d-basico/

    I hope you find it interesting

    Regards

  9. Star says:

    I’m sorry, my English is poor !I look at your book !I would like to ask you, CCDirectorIOS setView,Can repeat replace use? This problem bothering me for a long time, I hope you can help me answer as soon as possible or given scheme, thank you!

    • Hmmm I’m not sure what you mean. If you refer to replaceScene, a common mistake is to pass in the same (existing) scene object when you try to reload a scene. That won’t work, it has to be a new scene object.
      As for setView that refers to the cocos2d OpenGL view (CCGLView, formerly EAGLView) which you shouldn’t replace or remove at all.

      • Star says:

        Hi Steffen
        I don’t think I and you say clear my question, I elaborate on it. I want to tabbarViewController with two CCGLView, they need to how to switch? Use a CCDirectorIOS. View added to the two ViewController? Or by two CCGLView set a CCDirectorIOS? Or other solutions? CCDirectorIOS 可以重置吗?或者清空?希望您能帮我回答,谢谢!

      • Star says:

        Hello:
        Sorry,Just send a little mistake!

        Hello:

        I think I didn’t say I understand the problems, I’m sorry! The actual and you say me the difficulty they have ever met! I want to tabbarViewController with two CCGLView, they need to how to switch? Not exactly the first ViewController first page, the second ViewController secondary pages, so the two CCGLView won’t work together at the same time, so that should conform to the singleton pattern? I want to ask next solution with a CCDirectorIOS. View added to the two ViewController? Or by two CCGLView set a CCDirectorIOS? Or other solutions? CCDirectorIOS can reset or empty? I hope you can help me to answer these questions, thank you!

      • Star says:

        Hello:
        This is a CCDirectorIOS. View added to the two ViewController. Error content is:

        OpenGL error 0x0506 in -[CCSprite draw] 532
        OpenGL error 0x0506 in -[CCTextureAtlas drawNumberOfQuads:fromIndex:] 556
        OpenGL error 0x0506 in -[CCTextureAtlas drawNumberOfQuads:fromIndex:] 556
        OpenGL error 0x0506 in -[CCTextureAtlas drawNumberOfQuads:fromIndex:] 556
        OpenGL error 0x0506 in -[CCGLView swapBuffers] 280

        • You can’t have or use two CCGLViews, it’s as simple as that.

          Currently cocos2d is only capable of handling a single CCGLView. Allowing multiple OpenGL views has been on the roadmap for years but I doubt it’s a priority given the recent efforts towards cross-platform development.

          Your only option would be to re-use the same CCGLView for both tabs, and whenever you switch tabs you also change the cocos2d scene to display the contents for that particular tab.

          • Star says:

            Thank you for your answer

            According to your method, can only use [ViewController. View addSubView: CCDirectorIOS. View] method. But then I had a problem, the effect of operation in the simulator is normal, but to really machine sometimes switch to the current View, showing that it is a View, cause error is:

            OpenGL error 0 x0506 in – [CCSprite draw] 532

            OpenGL error 0 x0506 in – [CCTextureAtlas drawNumberOfQuads: fromIndex:] 556

            OpenGL error 0 x0506 in – [CCTextureAtlas drawNumberOfQuads: fromIndex:] 556

            OpenGL error 0 x0506 in – [CCTextureAtlas drawNumberOfQuads: fromIndex:] 556

            OpenGL error 0 x0506 in – [CCGLView swapBuffers] 280

            Do you know what reason this is? Thank you in spite of being very busy toglance help me to solve! Or you can provide a mail I sent to you, would you help me find out what’s the problem? My mailbox 234475462 @qq.com, thank you very much!

  10. Serge says:

    I just bought “Learn cocos2d 2″ and found the book very easy to understand.
    I am totally newbie in game development , but I was developer on the MacOS platform (both on shareware and pro applications… for example : http://www.lightwave-scientific.com/Resources/biOpen.pdf) in the past and I would like to start to develop some applications on IOS.
    Good job Steffen.

  11. Star says:

    Hello:

    I still meet the display problem, convenient to a EMial? I send you the past you to help me see, thank you very much!

  12. Hi everyone!

    “Learn & Master Cocos2D Game Development” is a great book! It helped me do a smooth transition from actionscript3 to iphone development.
    I bought the digital version and between the book and the examples, got to learn the basics well enough to release my first iOS game:

    https://itunes.apple.com/us/app/number-run/id563408572?mt=8

    thanks Stephen for an amazing book!

  13. David says:

    I just read your book and wonder there have simple method such as using “UITouch” event to detect and control the position of space ship in chapter 7 ? if yes, should I add a UI layer for detect the user touch action and pass to game layer to change ship position ?

    • It’s good practice to handle user input in a central location, and pass it on to objects in a specific order to either process the input or ignore it. It doesn’t matter where you handle and dispatch the input, a separate layer to do so would be fine.

      • David says:

        Thank you for your advise, although “ship” is CCsprite but it seem it is not allow using “boundingBox” method to detect touch on the ship, which method should I choice for detect the touch on the ship ? Any hints ?

  14. gustav says:

    Hi Steffen
    i just want to know if the source code and the things that are explained in the first edition of the book “learn iphone and ipad cocos2d game development” are compatible with xcode 4 or are only with xcode 3

    • The first edition used Xcode 3. It’s more or less compatible but Xcode 4 has vastly improved error reporting, so there will warnings/errors in both the book’s source code and cocos2d (v0.99.x and probably even v1.0.1) where there previously were none.

  15. songbh says:

    Instance method ‘-displaySizeInPixels’ not found (return type defaults to ‘id’),I don’t how to modify it
    my cocos2d is 2.0

  16. Breton Goers says:

    I’m curious about some things I have seen from your books. I bought the first book you had written (Learn Cocos2D with iOS 5), and have now seen this new one. First off, I’d like to say great books – very useful :)

    But my main question is this: in your book/projects, should we be worried about circular includes? For instance, in CH06, the GameLayer class includes the Ship class, and the Ship class includes the GameLayer class. I understand why you’ve chosen this design, I’m just curious if there is a better way without creating circular references?

    Or should we not care about things such as this?

    Thanks!

    • If you have a true circular reference, you’ll see a compile error. ;)
      If the #import is in the implementation (.m) file and not the header there is no risk for a circular reference.

      • Breton Goers says:

        Oh! Did not know that! But couldn’t the ship class just define a protocol and delegate that the GameLayer would implement?

        I suppose I am just a little puzzled. I think you mentioned in your book that the GameLayer is a pseudo-singleton, so from that perspective it makes sense that it would be accessible to the ship. But then I’ve always been told (as far as Obj-C goes) that a class contained within another class shouldn’t have direct knowledge of its parent, but instead use KVO, notifications, or delegates to communicate upwards. When and where is a good time to use which pattern then?

  17. Caroline says:

    I know you are very busy but I was wondering if you could possibly do a tutorial on how to make a word game like 4 pics 1 word :), thank you

  18. lehua says:

    hi,it is useful to learn develop game that your book.Thank you for your kindness

  19. Tom says:

    I was very impressed by your book – it just explain cocos2d in a very elegant way which makes it super easy to learn and fun, it´s first time I´v had fun learning a framework.

  20. TomL says:

    I am really scratching my head on which version of Cocos2D/Kobold to use for my project. I know that I will not use shaders, so there is really no reason to go for 2.x as I see it. But 1.x is legacy, so when iOS7 comes out there will be no update and maybe I´ll have to rewrite a lot of stuff myself… I honestly don´t know. I want my
    game to support as many devices as possible now, but also in the future…

    Do you have any recommendations? right now I am thinking it´s best to just go
    with 1.x. Apple tend to be good to developers as new versions of iOS usually just needs a few changes

    • Please don’t start new projects with cocos2d 1.x anymore!

      Like you said, the v1.x line has been retired and is unlikely to receive any attention from developers in the future. With new iOS devices coming, you can’t expect them to be supported by cocos2d 1.x or just not as well/fast as with cocos2d 2.x.

      Second of all, supporting iPhone 1st & 2nd generation accounts for perhaps 5% of the total market. Really not worth putting any extra time trying to optimize the performance of the app on those old devices. They’re significantly slower than even an iPhone 3GS.

      Plus the 1st/2nd generation devices run on iOS 4.2 (1st gen: 3.1) at most which means you need to do extra testing to make sure your app does not just crash on iOS 3 or 4. If you don’t have an iOS 3.x or 4.x device to test on you can not even compatibility-test your app since Xcode no longer comes with an iOS 3.x or 4.x Simulator anymore. All current devices can run at least iOS 5.1 (highest supported OS for iPod 3rd gen & iPad 1 – with the rest all supporting iOS 6.1).

      In general it makes very little sense to support more than the most current and the most recent iOS version given the growth in sales of new devices and the staggering upgrade rates of iOS device owners – meaning today your minimum deployment target should be iOS 5.1 with ARC enabled. Once iOS 7 comes out, I would wait for 3-6 months at most before retiring support for iOS 5.1 in both new and existing apps – unless for some reason you have a significant user base on iPod touch 3rd gen or iPad 1.

Leave a Reply

Powered by WishList Member - Membership Software