Cocos2d Iphone Resources

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:

  • 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 i made are licensed under Creative Commons BSD 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.

Leave a Reply

Your email address will not be published. Required fields are marked *