Knowledge Base

cocos2d, Cocoa and Objective-C code fragments

Have a look at my proposed CCDirector drawScene method which redraws the screen anytime you may need to.

Blog Post: efficiently reusing Default.png for landscape mode – Note: Ricardo has since integrated this in Sapus Tongue source code.

Integrating other APIs into a cocos2d application or game

First of all, here’s a tutorial on how to integrate the cocos2d API reference into Xcode.

Integrating Facebook Connect with cocos2d. See also the forum post for the user comments. In addition watch this video about adding Facebook Connect to your Xcode project since the necessary steps aren’t covered in the integration tutorial. And once again, Stackoverflow strikes with an excellent link collection for Facebook Connect iPhone developers.

Integrating AdMob with cocos2d iPhone Applications

Integrating OpenFeint with cocos2d iPhone Applications

Integrating Agon Online into cocos2d iPhone

Combining SIO2 with cocos2d, with source code

SpaceManager – an Objective-C wrapper for Chipmunk

There are already ports for cocos2d iPhone, one focuses to port cocos2d iPhone to Mono .NET using C# and the other cocos2d port is Java-based for Android development.

Generally useful tools & knowledge for cocos2d iPhone developers

How to handle device orientation and how to implement autorotation for a cocos2d app or game.

A list of editors useful for cocos2d development is described on the cocos2d’s homepage blog in two parts: part one and part two. It includes tile editors, Texture Atlas and Bitmap Font generators.

Ernesto’s pseudo-code on Pixel-Perfect collisions. It was hard to find and others seem to have problems finding it as well yet people already know it as “Ernesto’s post”, so it deserves a mention here. In this forum post is one implementation of pixel-perfect collision code and here is another implementation of pixel-perfect collisions. Finally we learn about the intricacies of endianness in pixel data, meaning how pixel data changes depending on the platform (iPhone vs Mac).

Zwoptex Texture Atlas tool for use with cocos2d iPhone (and probably others)

Finding memory leaks using the CLANG static analyzer. Don’t leak memory. That’s terrible. Read how you can run a program that tells you with impressive accuracy where there are memory leaks in your code, at least potential ones. Make sure you also read the comments further down since they contain more command line samples that fix some issues for iPhone OS 3.x among other things.

How to obtain the iPhone crashlogs from your beta-testers and costumers. And then there’s Apple’s manual on how to debug crashdumps. In short: always compile both debug and release builds at the same time, then keep both dSYM files and make sure you can later identify to which version and build they belong to. It’s easiest to use source control here and simply flag or label the version you used to create distributed Ad-Hoc or App Store versions with.

iPhone Apps can be opened via URLs that also allow passing what some might call “command line arguments”. Here are a few websites that document those URLs for various apps: URL schemes for various iPhone Apps, then there’s the handleOpenURL: website and finally AppLookup.com.

Specifically for linking to the App Store you will find Apple’s iTunes Link Maker useful. On Stackoverflow you can learn how to create links to your App that you can send via email. And finally, this Ars Technica article covers all the finer details of creating an App Store link that works for the iPhone’s App Store, including opening the App Store app with a search term – which is the only way to show all the apps of a particular company or developer on the iPhone’s App Store.

8 confusing  Objective-C Warnings and Errors explains some of the more confusing things Xcode (actually: GCC) throws at you and leaves you wondering. After reading this article you’ll know what you’re dealing with the next time it comes up.

Provisioning Profiles Gotchas – we all have one of these issues sooner or later (or frequently).

App Store Rejection reasons – be sure to go through this checklist before submitting your App to iTunes Connect to avoid your App being rejected due to some commonly made mistake or oversight.

How to build an IPA file from Xcode shows you how to setup a build target in Xcode that will output an IPA file. Those are very helpful for Ad-Hoc distribution of your App, as IPA files do not get as easily mangled (leading to errors such as “resources have been modified”), especially on Windows machines. It also makes installing Ad-Hoc builds easier since a simple double-click suffices to open iTunes and install the App. Plus it’ll have a proper icon in iTunes’ Application folder.

How to properly set your App’s iTunes Release Date and the things to watch out for. If you’re about to release an App you must read this otherwise you risk your App not being listed on the “What’s New” list.

How to calculate the App Store size of your iPhone App before approval. It’s not magic nor random, it can actually be calculated to within a rather small margin of error. The good thing is, the error will be that the calculation gives you the maximum size your App could possibly have on the App Store. With experience you’ll learn how good your Apps compress after approval, and that depends highly on the game engine used. Unity is hit especially hard by this, typically Unity apps get added another 4-5 MB to the zipped App you’re uploading, in some cases even more.

How to set and change the list of supported languages in iTunes: according to this post on Stackoverflow all you need is one localized file. What i do is, under Resources, i add a new .strings File and leave it empty. Then right-click, choose Get Info and on the  General tab click “Make File Localizable”. Then go back to the General tab and click “Add Localization” until you’ve added all languages your App supports. Note that you do not have to reference or otherwise use that file in your code – it just needs to be there. If you look into your App Bundle (right click: Show Package Contents) you’ll notice that new folders like English.lproj, German.lproj etc. exist. Those are scanned by iTunes to create the supported languages list.

How to change an App Icon’s name depending on the iPhone’s language setting. While we’re at it you can also learn how to figure out more quickly if a specific name still fits under the App Icon without being shortened.

My iPhone & iPad Design Templates for OpenOffice Draw

Open Office Draw Templates for designing iPhone & iPad games. They offer you a canvas to create the visual elements of your game and pitch your ideas. I use them to pitch my ideas or create concept drawings of iPhone/iPad apps and games.

You can either use Open Office Draw to draw on them, or just print out the images and draw on it. Both versions have at least one image where the screen background is white, so you can easily draw on it. The iPad version also has a vector graphics representation, so if you worry too much about the completely dark space for printing – turn it into light gray or even transparent with just the border lines shown.

Download the iPhone Design Template for OpenOffice Draw.

Download the iPad Design Template for OpenOffice Draw. For the iPad i took extra-care to scale the images to close to original size, and i also recreated the iPad as vector graphics in almost exact original size.

Tagged with:
 

Getting Started

cocos2d for starters

The cocos2d homepage – this should be obvious but since most people land on the google source code repository page for cocos2d iPhone instead of the real homepage www.cocos2d-iphone.org i wanted to post it here. Usually the most noteworthy threads from the forum as well as important blog posts by others are posted on the blog, and of course any new releases and updates to cocos2d.

cocos2d iPhone documentation – a post on the official website explains where you can find what kind of documentation.

Things i wish i knew when i started with cocos2d – Troubleshooting, Tips & Tricks for everyone

Ray Wenderlich (formerly Mythic Entertainment) posted a great tutorial on how to make a simple iPhone game with cocos2d. Take a look around at his website as he keeps writing new tutorialistic articles such as creating buttons in cocos2d and an intro to the box2d physics engine which is also part of cocos2d.

Introduction to 2D game programming with cocos2d iPhone

The cocos2d iPhone programming guide in the cocos2d Wiki.

Introduction to cocos2d iPhone visual effects (aka Visual FX).

Apple’s excellent Objective-C Programming Language reference (PDF)

Open GL ES 1.1 reference – only needed if you intend to do OpenGL programming – you don’t need to know about OpenGL if you’re using cocos2d but it may come in handy sometimes.

As for books, i can NOT recommend iPhone Games Projects by Apress. Repeat: not recommended! It’s a badly written collection of blog posts about the kind of games most game developers would not want to make (chess etc). It does not contain particularly good or reusable examples of code and the choice of projects is questionable to say the least. It has generally very little value for a cocos2d iPhone developer as all examples are either written in Cocoa Touch or plain OpenGL.

Tagged with:
 

Free Source Code

My shared cocos2d iPhone source code

Download all of the below cocos2d iPhone source code in one convenient ZIP file:

GamingHorror’s cocos2d-iPhone free SourceCode (.ZIP)

I’m providing generally useful classes for use with cocos2d which i’ve either written myself or taken from elsewhere (credit given as far as i know) and if necessary, modified it, enhanced it, fixed it. In all cases, i converted the format to regular Allman / ANSI style as that’s what i’m used to and i find it more readable. If you do prefer the K&R style it should be easy to convert, if necessary make use of a code formatting tool to assist you with that. I don’t know any – if you can recommend one, let me know and i’ll add it.

Currently, the code includes the following classes/methods:

March 9th, 2010:
  • Refactored PlayMusicAction to use SimpleAudioEngine (requires adding set…Listener method to cocos2d’s SimpleAudioEngine).
Since February 24th, 2010:
  • AppStoreHelper – open App Store to show all Apps made by a specific developer or company. Has methods for creating both iTunes links for the computer and on the iPhone’s App Store, which requires the link to be created differently.
  • BundleHelper – conveniently access the keys of the App’s info.plist, for example to get the App’s version number.
  • LargeTextureLoader – work in progress – is supposed to load (larger) textures one by one via a scheduled method. I started this because textures take twice the memory in the frame they are loaded, if you load several larger textures that amount to 10 MB total but you load them all in the same frame, then this frame will require 20 MB and you may receive memory warnings. The solution is to wait a short while after each texture so cocos2d can free up the temporary memory. Again: work in progress but the concept is there.
  • RemoveFromParentActioni found this on the cocos2d forum, it’s a neat solution to have a CCNode remove itself from its parent when the action executes. Very convenient.
  • SimpleButton – it wraps a CCMenu and a CCMenuItem to create a menu with just one item: a simple button. Makes it easier to create single buttons when you don’t need or can’t use a proper CCMenu, for example if your buttons are spread all over the screen, or have move or wiggle independently.
  • TextureAtlasManager – wraps the CCSpriteFrameCache to have more convenient access to it, getting a sprite by name, removing textures from the cache and the intention is to make it work with the LargeTextureLoader to avoid the huge memory usage if you have to load several Texture Atlas textures.
Since February 14th, 2010:
  • LayoutHelper – touch and drag sprites around to layout them visually instead of manually changing coordinates. Refer to Blog Post for more info.
  • PlayMusicAction – a CCAction that lets you queue several MP3 files which are played in succession.Refer to Blog Post for more info.
  • AlertPrompt – shows an AlertView that asks the user to enter some text. Original code by Jeff LaMarche, i made a small modification to the height of the view (a bit hack’ish but it works … you’ll know it when you see it). Maybe that issue only appears if you’re in landscape mode. And if you know of a better way, or how to pre-fill the textbox with some text please let me know.
  • SendMail with CocoaHelper – allows you to use In-App Mail in any cocos2d app, and it works correctly both in Portrait and Landscape mode. You might want to read my quest to get In-App Mail working in Landscape mode. The SendMail and CocoaHelper code are originally put together from fragments i found in the cocos2d forum but i since have lost the links. However, the code has changed so much over time that i think i can now call it my own. Let me know if you want to be given credit for the original Cocoa integration and SendMail code fragments and i’ll add the link here. CocoaHelper’s static methods allows you to display/hide any ViewController’s views on top of cocos2d.
  • LocaleHelper – makes localization of your app easier. These static methods allow you to get the preferred language, get/set a specific language and get/set the language string (“en”, “de”, etc.). It currently supports only english and german but can be easily enhanced to support more languages – you just need to add it to the language enum and the language’s string identifier. I use it in SendMail to get the correct language strings via NSLocalizedStringFromTable for the “email is not setup on this device” AlertView.
  • MenuGrid – it bothered me that CCMenu only aligned menu items either horizontally or vertically, i needed a grid! Since i was not successful in subclassing CCMenu i simply copied what i needed from CCMenu and enhanced it so that i could align my CCMenuItem sprites in a grid layout. I used it to display the tiny 51 Japanese Characters where they are all on the screen at once. In hindsight i probably could have done it by using several CCMenu next to each other but i think there was some reason to do it with a MenuGrid instead, i can’t remember why though.
  • Screenshot – static methods for creating a screenshot in UIImage, CCTexture2D and PNG (NSData) format. Refer to original cocos2d forum post by manucorporat for more info. This code is intended to be included into cocos2d so the code may already be integrated in cocos2d’s CCDirector by the time you read this.
  • ShakeHelper – detects screen shakes and calls a delegate. The original code was posted on StackOverflow by millenomi, developer of the Diceshaker App.
  • Utilities – a collection of useful static methods. I made a method that cleans out certain short-lived textures from CCTextureCache to preserve memory. Textures or Sprites that are given the particular kDontCacheTexture tag can be removed from CCTextureCache with a single call anytime you want. Another method gets a random number that is in a range but excludes a particular number, useful when you want a random number that is different than the one you currently use. And lastly i found some code to get the available free memory on the device. Print this on a CCLabel to see if you’re low on memory or how much memory you saved by using the kDontCacheTexture tag or by making other changes, for example switching to a TextureAtlas.

All of my own source code and modifications are licensed under:
Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 Unported License.

Code originally created by others sometimes does not have any explicit license attached to it – but since it was posted in forums, blog posts and other public places i think it’s safe to assume it to be free, as in public domain. If it is not, and you would like your code to be removed or put under a specific license, let me know and i’ll change it.

Tagged with:
 

Starter Kits

Line-Drawing Game Starter Kit for cocos2d-iPhone

Coming Soon! No release date set. When it’s done great value.

How much progress i can make depends on amount of contract work. Check back here for updates or read the preliminary documentation for the Line-Drawing Game Starter Kit. On the Main Page i keep track of the progress. And check out the Line-Drawing Game Starter Kit project page on codaset, as a buying customer i will grant you access to the source code repository if you have a codaset account (it’s free to join), so you can always stay up to date via GIT and report issues or request features.

I also intend to release a sample game to the App Store rather soon, so that you can download and try out the Starter Kit (and maybe even report bugs if you’re kind enough). I’ll keep making updates to the App so that you can stay up-to-date with the latest progress just by running the App from time to time. I’ll make an announcement when the App is submitted to the App Store.

Apple’s iPad has 256 MB of RAM

It’s a simple calculation if you take the iPhone 3GS as baseline, which has 256 MB of memory. If we look at the numbers of available memory on the iPhone 3GS we notice that the maximum free memory on the 3GS is close to 150 MB – if you want to be in the region where you don’t want to encounter memory warnings you need to be below about 120 MB.

I know that iPad developers reported iPad App rejections (no sources given to protect the innocent) when memory usage was between 120 to 150 MB. Unsurprisingly this is also in the range where you get memory warnings or crashes on the iPhone 3GS. That leaves me to conclude that the iPad has 256 MB of RAM and not 512 MB as stated on Wikipedia (without citation).

It’s still plenty to work with though even if you consider the larger framebuffers, 6 MB on the iPad compared to the 1.2 MB on the iPhone 3GS for two framebuffers. You have 5 MB less to work with compared to the 3GS, that’s at most 4% less available memory.

On a related matter – would you like to see the iPad taken apart piece by piece? They conclude by the memory chips that the iPad does have 512 MB by decoding the chip numbers. I’m not so sure about that – it’s two 2 Gigabit chips apparently making 512 MB but then the question is: why do we developers see the same free amount of memory as on the iPhone 3GS? Why and how does the iPad keep the remaining 256 MB to itself? I remain doubtful that it’s really 512 MB.

For us developers, the device seems to offer only the same free amount of memory than we have on the iPhone 3GS – so it doesn’t really matter whether the iPad has 512 MB or 256 MB RAM in hardware.

CONFIRMED: iFixit have since corrected themselves and now say that the iPad has indeed 256 MB of memory.

Tagged with:
 
Page 1 of 3123
© 2010 Steffen Itterheim aka Gaming Horror