The Cocos2D Podcast is a great resource if you are interested in industry leading developers and what they are doing. I listen to it regularly, and was surprised and honored when Azam asked me to be a guest on the show to talk about Augmented Reality. This past Saturday we recorded the show, and yesterday it became available. Thank you Azam and Steffen for having me on!

Check it out!
http://cocos2dpodcast.wordpress.com/2011/12/06/augmented-reality-with-nick-waynik/



When working on a project, it was brought to my attention that the app’s custom navigation bar image wasn’t displaying. After looking at the code, the previous developer was using the drawRect: method to set the image. This no longer works in iOS 5 because the method never gets called, unless you subclass UINavigationBar. Below is a snippet of code to get the image to display on iOS 5 devices.

*UPDATE: Use run-time checking!*

float version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (version >= 5.0) {
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"portrait_image.png"] forBarMetrics: UIBarMetricsDefault];
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"landscape_image.png"] forBarMetrics:UIBarMetricsLandscapePhone];
    [[UIBarButtonItem appearance] setTintColor:[UIColor colorWithRed:201.0/255 green:31.0/255 blue:56.0/255 alpha:1]];
}


Just about every app that I work on utilizes NSUserDefaults in one way or another. It is quick to implement and very easy to use.

First you need to create an NSUserDefaults variable:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

Next you will need to save something that can be accessed at another time. This is very easy, we will use set methods to set values for keys. The keys can be named whatever you like, I named mine based on what object type I’m saving for this demo. Here we will save quite a few different object types:

// NSString
[userDefaults setObject:@"Your text here" forKey:@"StringKey"];
 
// Float
[userDefaults setFloat:5.22 forKey:@"FloatKey"];
 
// Double
[userDefaults setDouble:6.1234 forKey:@"DoubleKey"];
 
// NSInteger
[userDefaults setInteger:7 forKey:@"IntegerKey"];

After saving objects to your NSUserDefaults, you will want to run the the synchronize method.

[userDefaults synchronize];

Next up we will retrieve values for the keys we saved above. Here we will get the object type for the keys we specified above and assign it to a variable.

// NSString
NSString *myString = [userDefaults stringForKey:@"keyToLookupString"];
 
// Float
float myFloat = [userDefaults floatForKey:@"floatKey"];
 
// Double
double myDouble = [userDefaults doubleForKey:@"DoubleKey"];
 
// NSInteger
NSInteger myInteger = [userDefaults integerForKey:@"integerKey"];

And that is it! Hope you enjoyed this Quick Tip.



I upgraded to Mac OS X Lion last month and there has been quite a few changes to get used to. One thing I noticed was that my clear key on the full keyboard doesn’t work anymore in the Calculator widget. After a little searching, I found a solution!

1. Open up a finder window and navigate to the following folder:

/Library/Widgets

2. Next locate Calculator.wdgt and ctrl-click on it, then select “Show Package Contents.”
3. Copy Calculator.js to some other location such as your desktop.
4. Open the file up in any text editor.
5. Goto line 127, and change “case 63289″ to “case 61705″. It should look like this when you are done.

case 61705:
case 27:
	key = "c";
	break;

6. Close and save the file.
7. Copy the file back to the contents of Calculator.wdgt.
8. Fire up dashboard and remove your current calculator widget and add a new one.

You’re clear button will now work in Calculator!

Here is the link to the original solution.



I use my user library folder all the time, and I’m a little bit annoyed that the Library folder is now hidden in Mac OS X Lion. Here is how I changed that!

Open up Terminal and type the following and tap return:

chflags nohidden /users/<insert your username here>/library

Note: Make sure you substitute your username in that command.



I have been doing some work with JSON recently and it can be very difficult to wade through the results. This snippet of code can very easily show you keys and values for objects within a NSDictionary.

NSString *key;
for(key in jsonDictionary){
     NSLog(@"Key: %@, Value %@", key, [jsonDictionary objectForKey: key]);
}

I hope this helps out!



Have you ever wanted to change or swap out an image assigned to a sprite?  Well, it’s very easy to do!

We will start out by adding a Sprite Batch Node to the a layer, then we will add a new sprite.

// Add Sprite Batch Node
CCSpriteBatchNode *batchNode = [CCSpriteBatchNode batchNodeWithFile:@"Sprites.pvr.ccz"];
[self addChild:batchNode];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"Sprites.plist"];
 
// Add new sprite
CCSprite *sprite1 = [CCSprite spriteWithSpriteFrameName:@"SpriteName0.png"];
sprite1.position = ccp(240,160);
[self addChild:sprite1];

All we need to do to change the image used by the sprite is to set it’s display frame like so:

[sprite1 setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"SpriteName1.png"]];

And that’s a wrap! I hope you enjoyed this quick tip, and I will be adding one a week. If you have any suggestions, please add them in the comments section.



Here is the pdf version of the slides and a zip of the project.

Intro To Cocos2D Slides

TestApp Project



AR with Cocos2DThe tutorial I created for Ray Wenderlich’s website is now live! Click here to view it.

Here is a snippet of the beginning:

In this tutorial you will learn how to make a simple augmented reality game for the iPhone/iPod touch!
In this game you will utilize the camera, gyroscope, and Cocos2d framework. Sounds exciting right?
It definitely was fun exploring these technologies while writing this article. There is a bit of math and conversions, but don’t worry – it’s nothing too hard!

To use this tutorial, you need an iPhone 4 because the tutorial uses the gyroscope to move your view of the world.
You also need to have at least basic knowledge of Cocos2D and have Cocos2D installed. If you are a complete beginner to Cocos2D, you should go through some of the other Cocos2D tutorials on this site first.

Are you ready to start blasting some virtual aliens? Let’s go!



A sample video of a tutorial I’m working on for Ray Wenderlich’s website.