GKPeerPickerControllerDelegate and Cocos2D workaround

The iPhone SDK 3.0  introduced a GKPeerPickerControllerDelegate which can be found in the GameKit framework. That peek picker is great if you want to create a multiplayer game.

Game you said? So I will use the GameKit framework to establish a connection between my two (or more) players and I will use Cocos2D for my GUI…

The problem is, a Cocos2D view is not an UIView and our peer picker has been made to be running on top of an UIView. So we see the picker, we can cancel it, we can see the other devices in a nice (but not responding very well) tableView inside the picker BUT it’s not possible to choose one. In other words : the UITableView embeded in the picker is not working well because we added the picker in a Cocos2D view.

The first solution is to put a UIView (running the picker) on top of the Cocos2D view… I tested it and crap, it’s not working, the UITableView is still not responding.

My solution :

  • dismiss the Cocos2D view - [[Director sharedDirector] end];
  • add a UIView running the peer picker - [[[UIApplication sharedApplication] keyWindow] addSubview:[[[[ConnectionManagerViewController alloc] init] autorelease] view]];
  • when you are ready to go back to a Cocos2D view, you have first to (re)attache the Director to the application’s window - [[Director sharedDirector] attachInWindow:[[UIApplication sharedApplication] keyWindow]];
  • then you simply run your multiplayer scene - [[Director sharedDirector] runWithScene:[GameScene node]];

I will soon be writing more about the GameKit framework and Cocos2D with UIKit Views…

Cocos2D – Menu

Cocos2D for iPhone is great for 2D game developpement on the iPhone. And what’s the first thing a user will see when he starts your game? That’s right, a menu!

Create a menu with cocos2D is really simple. You first create a Scene and a Layer for your menu (with a nice background image if possible). Then you can create your menu items (MenuItem class) :

MenuItem *startGame = [MenuItemFont itemFromString:@"Start Game" target:self selector:@selector(startGame)];

MenuItem *help  = [MenuItemFont itemFromString:@"Help" target:self selector:@selector(help)];

MenuItem *about  = [MenuItemFont itemFromString:@"About" target:self selector:@selector(about)];

When you menu items are created, you just need to add them in your menu (Menu class) and customize the layout :

Menu *menu = [Menu menuWithItems: startGame, help, about, nil];

[menu alignItemsVertically];

Finally, you add your menu to your layer :

[self addChild:menu];

That’s it your menu has been created. This code snippets don’t give the implementation of the methods triggered when pressing the buttons (target: selector: ) but here is what you will probably do :

-(void)startGame{

[[Director sharedDirector] replaceScene:[GameScene node]];

}

But you can do whatever you want e.g. create a transition or a UIKit view. But that’s another story…

UITableViewCell usage : easier since iPhone OS 3.0

Since iPhone OS 3.0b, my previews code snippets witch were using table view are marked deprecated where I trie to access the text property of a UITableViewCell. When I looked at the 3.0 documentation, I saw

text

The text of the cell. (Deprecated. Use the textLabel and detailTextLabel properties instead.)

The message is pretty clear : don’t use the text property, use the textLabel or detailTextLabel property.

So now why did Apple’s developer changed the text property into a textLabel and a detailTextLabel? The answer is : to make use of the UITabelCell easier. I remember I had a bad experience when I tried to customize a cell to be able to show a principal text with a secondary text just under and an image on the right side of the cell.

Now, when you create your cell, you fill it like this :

  • cell.textLabel.text = @”Title”;
  • cell.detailTextLabel.text = @”Detail”;
  • cell.imageView.image = [UIImage imageNamed:@"image.png"];

That’s it. We’ve set up a great looking custom cell for our UITableView.

How to convert a chm file into a pdf file on a Mac

Recently I downloaded a lot of e-books (don’t tell anybody because I think it’s not legal) talking about Cocoa, Objective-C and OpenGL ES. There were a lot of pdf files but also some chm files. For those like me who are not aware of what is a chm file, here is the link to wikipedia I have to say I stopped reading when I saw Microsoft for the first time (which means I stopped reading at the first word).

Ok so now you know what is a chm file and why you cannot read it with your mac without a third party software who can convert a crapy file into a portable file format. I tried 3 softwares and only one of them was able to show me the file in a sweet graphical interface (still crapy to read this kind of file but ok) and to convert the file into a pdf file.

This software is iChm. Find it, install it and enjoy!

To convert the chm file into a pdf file, just choose ->File->Save as PDF…

iChm

Easy!

Using SQLite3 C Api in Cocoa with xCode

First you need to import the libraries :

  1. Right click on the Other Fraleworks group inside the Fraleworks group
  2. Choose to Add an Existing File
  3. Go inside /Developer/SDKs/MacOSX10.5.sdk/usr/lib/ to choose the libsqlite3.0.dylib file (the path can be different)
  4. You can choose to Copy items into destination group’s folder

screen-capture-1.png

Then you can use the SQLite C api like this (snippet) :

#import “Test.h”

#import “sqlite3.h”

@implementation Test

- (id)init

{

self = [super init];

if (self) {

[self openDatabaseAt:@"/tmp/test.db"];.

}

return self;

}

-(BOOL)openDatabaseAt:(NSString*)path

{

sqlite3* db;

NSFileManager *fileManager = [NSFileManager defaultManager];

if([fileManager fileExistsAtPath:path]){

int open = sqlite3_open(path, &db);

if(open == SQLITE_OK){

NSLog(@”Database was opened at %@”, path);

return YES;

}else{

NSLog(@”Database found at %@ but the database couldn’t be opened”, path);

sqlite3_close(db);

return NO;

}

}else{

NSLog(@”Database not found at %@”, path);

return NO;

}

}

@end