Review: Cocos2d for iPhone 1 Game Development Cookbook


“Cocos2d for iPhone 1 Game Development Cookbook” is written by Nathan Burba (@nathanburba).

According to the description, it has “over 90 recipes for iOS 2D game development using cocos2d”. and just by looking at it’s Table of Contents, you’d see that this book covers a lot of topics (some advanced techniques that other cocos2d books have not covered). It also provides solutions to some common problems that we developers might encounter during Cocos2d iOS development.
Like in Chapter 1, it covers graphics, but unlike other books that would only teach you how to make sprites and spritesheets, this book even teaches you how to use apply mipmapping so that that when you scale up or down a sprite it wouldn’t look pixelated. It also has a recipe for a cool technique by swapping palettes so that even with limited amount of art, you can create various versions of a sprite (a single baseball player texture can have be used to create different baseball players with different colored shirts and pants etc). Chapter 1 also answers some questions that you might encounter while working on your game, such as how to I play a video clip of my company’s logo or my game’s cutscene (playing video files recipe). It also covers how to render 3D objects, drawing OpenGL primitives, particles, etc.
Chapter 2 is all about User Input on iOS devices, including the usual (tap, hold, drag), making virtual buttons (creating an analog stick recipe and directional pad recipe) and using the accelerometer. One really cool (for me) recipe that’s also included is gestures (you’d be able to tell what shape the user drew on the screen)!
Chapter 3 is about Files and Data. In making games, we’d need ways to store data (just us score, or level design, etc) and this chapter covers different ways for us to do this. This chapter has recipes for reading plist, json, xml data files, it also includes recipes about archiving objects, saving data using plist, SQLite, Core Data, etc.
Chapter 4 is about physics. Cocos2d has two physics engines that we can use Box2d and Chipmunk. This book only covers Box2d (which is the engine that Angry Birds used), it includes detailed explanations and recipes on how to set up your Box2d environment and simulating physics properties. it even includes a recipe on how to make a car drive up a bumpy hill road!
Chapter 5 is about Scenes and Menus. It also includes how to create buttons and labels (with fancy shadow effects even)! It also has a recipe for wrapping the UIKIT and using it in our Cocos2d game.
Chapter 6 is all about Audio: how to play background sound effects, play music, using the iPod music library. But the really fun bits about this chapter is it even teaches you how to record audio, create a MIDI synthesizer, and to top it all off, it even covers spech recognition and text to speech! Imagine all the games and apps that you can make with those things.
Chapter 7 covers AI, including waypoints, flocking using boids and A* pathfinding. it also teaches you how to run lua scripts and use lua scripts for dialog trees.
Chapter 8 is called Tips, Tools and Ports, and sure enough it includes information about tools that can be used for our Cocos2d game development. Best part, it also includes detailed step by step instructions of how to put your game on the AppStore!
Overall this is a good book with a lot of useful and varied information that have not been covered in other books (and are often lacking in online tutorials). Also the best thing about this book is it comes with complete working code and detailed explanations on how the code works.
Except some of the topics covered in this book are a bit advanced, so this book is not recommended for beginners, basic Objective C and Cocos2d knowledge is required.
Also the author, Nathan is also very active in the Cocos2d forums, if you have any questions about the book you can just ask him, and he almost always responds immediately.
So if you’re interested to learn new things about Cocos2d iOS development, do check it out! Also if you are interested in this book, I am holding a giveaway contest (where I am giving away 3-4 copies of the book), join now (it’s only until this Wednesday). For more details read my previous blog post (http://purplelilgirl.tumblr.com/post/15713172087/cocos2d-for-iphone-1-game-development-cookbook-giveaway)
Link: http://www.packtpub.com/cocos2d-for-iphone-1-game-development-cookbook/book

Tutorial: The first step to making a ‘Talking’ iPhone app, chipmunkifying your voice!


Repost from my tumblr: http://purplelilgirl.tumblr.com/

Yah, I invented a word, “chipmunkifying”, or to make your voice sound like a chipmunk. Why? ‘Talking’ apps are all over the place in the iPhone app store, there’s a talking tom cat, talking bird, talking hippo, talking giraffe, whatever animal you can think of… Basically what those apps do is, you say something, and then the animal will repeat it, in this chipmunk like voice. Oh, you can poke, tickle, hit it too, or whatever…

Oh, I am using Cocos2D as my game engine. So, begin by creating a project using the Cocos2D application template. If you are not familiar with Cocos2D, you can go to its website for instructions on how to download and install it.

Well, so the first step to a ‘Talking’ app is of course, it has to record what you say.

I’ll be using AVAudioRecorder to record my voice, it’s really simple to set up, just follow the intructions on this blog by Jake Wyman. But he uses the iPhone SDK, while I will be using Cocos2D. So just follow his tutorial up to the part of adding frameworks:

From your XCode interface you are going to select the Frameworks folder, ctl>click and choose ADD and then select Existing Frameworks… Then choose both the CoreAudio.Framework and the AVFoundation.Framework

And after we have done that, some coding… Rather copy paste some code from Jake’s.

First create a NSObject class, named AudioController.

Import AVFoundation and CoreAudio:

#import <AVFoundation/AVFoundation.h>
#import <CoreAudio/CoreAudioTypes.h>

Set up AudioController as a AVAudioRecorderDelegate. And declare an AVAudioRecorder, and a string for recordedTmp (the file path where we will temporarily store our audio).

@interface AudioController : NSObject <AVAudioRecorderDelegate>

{   AVAudioRecorder * recorder;

NSString *recordedTmpFile;

}

We instantiate an AVAudioSession in a function called initAudioController (basically the code inside Jake’s viewDidLoad):

– (void) initAudioController
{   //Instanciate an instance of the AVAudioSession object.
AVAudioSession * audioSession = [AVAudioSession sharedInstance];
//Setup the audioSession for playback and record.
//We could just use record and then switch it to playback leter, but
//since we are going to do both lets set it up once.
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: &error];
//Activate the session
[audioSession setActive:YES error: &error];
}

And then our record function:

-(void) record
{   NSMutableDictionary* recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];

recordedTmpFile = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent: [NSString stringWithString: @”recording.caf”]]];

recorder = [[ AVAudioRecorder alloc] initWithURL:recordedTmpFile settings:recordSetting error:&error];

[recorder setDelegate:self];
[recorder prepareToRecord];
[recorder record];
}

And now, to play whatever recorded in a chipmunk voice. In Jake’s project he uses AVAudioPlayer to play his sound, but that isn’t going to work for me, because AVAudioPlayer doesn’t allow me to change the playback speed.

So instead of using that, I will be using CocosDenshion’s CDSoundEngine. I am reading Chapter 9 : Playing Sounds With CocosDenshion of Cocos2d for iPhone 0.99 Beginner’s Guide:

According to Pablo Ruiz, we need to import more frameworks to get CDSoundEngine working:

… include OpenAL and AudioToolbox frameworks in your project.

More imports:

#import “cocos2d.h”
#import “CocosDenshion.h”

And then declare a CDSoundEngine:

CDSoundEngine *soundEngine;

In initAudioController function, we initialize the soundEngine.

soundEngine = [[CDSoundEngine alloc] init: kAudioSessionCategory_PlayAndRecord];

NSArray *defs = [NSArray arrayWithObjects: [NSNumber numberWithInt:1],nil];
[soundEngine defineSourceGroups:defs];

And then we play:

-(void) play
{   NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent: [NSString stringWithString: @”recording.caf”]];

[soundEngine loadBuffer: recordedTmpFileIdx filePath: filePath];
[soundEngine playSound: recordedTmpFileIdx sourceGroupId: 0 pitch: 2.0f pan: 0.0f gain: 1.0f loop: NO];
}

Take note of the pitch property: it says 2.0f. What does it mean? The setting for normal pitch is 1.0f, if you increase its value, you get a higher pitch, also known as a chipmunked voice, if you decrease the pitch, you get this low kind of creepy voice.

We also need to make a function for stopping the recording and then start playing:

-(void) stopRecording
{   [recorder stop];
[self play];
}

And then we make a function for unloading the AudioController:

– (void) unloadAudioController
{   NSFileManager * fm = [NSFileManager defaultManager];
[fm removeItemAtPath:[recordedTmpFiles [0] path] error:&error];
[recorder dealloc];
recorder = nil;
[soundEngine release];

}

Okay, now we have AudioController done, it’s time to call it in our HelloWorld scene. Yes. the HelloWorld that comes with the default template. I also added a BOOL isRecording to keep trakc if we are recording or playing.

HelloWorld.h:

#import “cocos2d.h”
#import “AudioController.h”

// HelloWorld Layer
@interface HelloWorld : CCLayer
{    AudioController *audioLayer;

BOOL *isRecording;
}

+(id) scene;

@end

For HelloWorld.m, in init, add swallowedTouches and change the “Hello World” label to “Say something…” or “Speak!” or “Talk to me” or whatever.

[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];

CCLabel* label = [CCLabel labelWithString:@”Say something…” fontName:@”Marker Felt” fontSize:32];

Also in init, initialize audioLayer, and set isRecording to NO

audioLayer = [[AudioController alloc] init];
[audioLayer initAudioController];

isRecording = NO;

And then, since I am lazy to add buttons, the user simply taps the iPhone, anywhere on the iPhone once, to record and then tap again to stop recording and play the audio.

– (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event

{    if(isRecording)

{    [audioLayer stopRecording];

isRecording = NO;

}

else

{       [audioLayer record];

isRecording = YES;

}

}

And in HelloWorld’s dealloc add:

[audioLayer unloadAudioController];

And that’s it 🙂

You can record your voice and play to sound like a chipmunk 🙂

For any questions (or if you find any errors), feel free to contact me through Twitter, here, email, Facebook or whatever.

Facebook Twitter EMail

Yeah for free books!


If you noticed on my sidebar, there is a list of technical books:

Cocos2d for iPhone 0.99 Beginner’s Guide

From the site:

Overview of Cocos2d for iPhone 0.99 Beginner’s Guide

  • A cool guide to learning cocos2d with iPhone to get you into the iPhone game industry quickly
  • Learn all the aspects of cocos2d while building three different games
  • Add a lot of trendy features such as particles and tilemaps to your games to captivate your players
  • Full of illustrations, diagrams, and tips for building iPhone games, with clear step-by-step instructions and practical examples

Link: http://link.packtpub.com/c65cvt


WS-BPEL 2.0 for SOA Composite Applications with Oracle SOA Suite 11g

From the site:

Overview of WS-BPEL 2.0 for SOA Composite Applications with Oracle SOA Suite 11g

  • Develop BPEL and SOA composite solutions with Oracle SOA Suite 11g
  • Efficiently automate business processes with WS-BPEL 2.0 and develop SOA composite applications.
  • Get familiar with basic and advanced BPEL 2.0.
  • Detailed explanation of advanced topics, such as security, transactions, human workflow, process monitoring, transition from BPMN to BPEL, dynamic processes, fault handling, etc., enabling you to work smarter and more efficiently.
  • Step-by-step real-world examples to learn and manage various advanced aspects of BPEL development using Oracle SOA Suite 11g, JDeveloper, Oracle Service Bus, Oracle Service Registry, Oracle Enterprise Repository, BPM Suite and BPA Suite

Link: http://link.packtpub.com/jPKexr


Yahoo! User Interface Library 2.x Cookbook

From the site:

Overview of Yahoo! User Interface Library 2.x Cookbook

  • Easily develop feature-rich internet applications to interact with the user using various built-in components of YUI library
  • Simple and powerful recipes explaining how to use and implement YUI 2.x components
  • Gain a thorough understanding of the YUI tools
  • Plenty of example code to help you improve your coding and productivity with the YUI Library
  • Hands-on solutions that takes a practical approach to recipes

Link: http://link.packtpub.com/oVCUqU


Magento 1.4 Development Cookbook

From the site:

Overview of Magento 1.4 Development Cookbook

  • Develop Modules and Extensions for Magento 1.4 using PHP with ease
  • Socialize your store by writing custom modules and widgets to drive in more customers
  • Achieve a tremendous performance boost by applying powerful techniques such as YSlow, PageSpeed, and Siege

Link: http://link.packtpub.com/tfzwak


Magento 1.4 Themes Design

From the site:

Overview of Magento 1.4 Themes Design

  • Install and configure Magento 1.4 and learn the fundamental principles behind Magento themes
  • Customize the appearance of your Magento 1.4 e-commerce store with Magento’s powerful theming engine by changing Magento templates, skin files and layout files
  • Change the basics of your Magento theme from the logo of your store to the color scheme of your theme
  • Integrate popular social media aspects such as Twitter and Facebook into your Magento store

Link: http://link.packtpub.com/8Fgdx4


That’s because I recently joined the Packt Partnership program, which perks includes, well, free books! 🙂 They also asked me if I can host a contest on my blog to give away free books. Why not? So yeah, I’ll be hosting a contest soon, details to follow.

How to make fancy labels using Cocos2D?


Crosspost from my tumblr: http://purplelilgirl.tumblr.com

A very short tutorial.

I’m reading Cocos2D for iPhone Beginner’s Guide, and I’m reading Chapter 4 right now, called Pasting Labels, because I’m working on my game’s HUD. According to the book, there are three ways for displaying text in Cocos2D, first is CCLabel (which according to the author, is not very efficient, also it only supports the iOS fonts, which are Arial, Helvetica… which are not very much), and then there’s CCLabelAtlas (which is getting the characters from an image, you can do a lot of fancy effects using this, but the fonts has to be fixed width), and then there’s option number 3, which is CCBitmapFontAtlas.

The book introduced the Hiero Bitmap Font Tool for making CCBitmapFontAtlas (download link: http://slick.cokeandcode.com/demos/hiero.jnlp). So basically, I just followed the instructions in the book: download the software, run the software…

The book gave examples on how to use the Effects in Hiero, but I don’t find it sufficient, so I’m going to leave it plain, but I will add a bit of padding on the side (3, 3, 3, 3).

Go to File-> Save BMP font files…

It will save a .fnt and a .png. For some odd reason my .pngs are always inverted, so I open it in Photoshop…

I added the background, so you can see my text clearer. See, it’s inverted, so I just go to Image-> Image Rotation-> Flip Canvas Vertical.

The next step is double click on the Layer of the text or go to Layer-> Layer Style-> …

And add some fancy layer effects to your text. I only added Drop Shadow, but you can all sorts of fancy stuff, like Outer Glow, Bevel…

And then your text will look like this:

Save your file.

Drag the edited .png file and the .fnt file to your XCode project.

Some code for the CCBitmapFontAtlas:

CCBitmapFontAtlas * lblHighScoreTitle = [CCBitmapFontAtlas bitmapFontAtlasWithString:@”HIGH SCORE: ” fntFile:@”helveticaCY.fnt”];

And voila! You’re good to go!

This book is written by Pablo Ruiz a…


This book is written by Pablo Ruiz, a very active member of the Cocos2D iPhone community forums, he even helped me through my Cocos2d iPhone development.This book is written for people beginning iPhone development using Cocos2D, but it assumes that the readers already have a background in programming. So if you never programmed before in your life, this book may be a bit of an information overload.

This book starts from the beginning, from downloading to installing Cocos2D and getting example projects to work. It also explains the basic structure of a Cocos2D game.

This book will actually teach you to do three different types of games, a puzzle game (think Bejewelled), a shooter game (think Espagaluda, but using the accelerometer as controls), and a physics game, he calls Totem Balance (which reminds me of a Unity3D game called  TumbleDrop).

But, if you are looking for a book with codes that you can just copy and paste to make your first game, this may not be it. This book was written before the new version of Cocos2D was released (0.99.5), so if you are downloaded the latest version of Cocos2D, and copy pasted the codes from the book, there are some that may not work out as planned. After each code bit, there is this What just Happened section wherein the author explains what happens with the code. So if you read through the explanations, you get the theory of how things work. Besides, if you really get stuck, you can simply drop by the forums, and the author, along with all the other helpful forum people is more than willing to help you.

Aside from the example games, the book also covers a lot of topics. He even covers a lot of little details that I did not think a beginner needs to know about, but helpful nonetheless. Like, pixel formats, when I first started Cocos2D, I did not really care much about the pixel formats that each of my sprites are using, but of course, when I started looking for ways to optimize my project, that’s when I find that information very useful. So I guess, if I had known about that early on, I would have had those problems with optimization. So kudos to the author for including those details.

Overall, I think this book is a very comprehensive guide to anyone, who has a bit of background in programming, and wants to start making games using Cocos2D.

Link: http://link.packtpub.com/1OZzbp

A new book on Cocos2D by Pablo Ruiz…


A new book on Cocos2D by Pablo Ruiz.

Book link: http://link.packtpub.com/1OZzbp

From the website:

Overview of Cocos2d for iPhone 0.99 Beginner’s Guide

  • A cool guide to learning cocos2d with iPhone to get you into the iPhone game industry quickly
  • Learn all the aspects of cocos2d while building three different games
  • Add a lot of trendy features such as particles and tilemaps to your games to captivate your players
  • Full of illustrations, diagrams, and tips for building iPhone games, with clear step-by-step instructions and practical examples

Review coming soon.