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 left 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

How to create an Axis ARchive with Ant

When you are developing a web service with Apache Axis2, you know you can drop an Axis ARchive (aar) inside you Axis2 home directory or deploy this aar from the administration console. But first you need to create this archive! Some people do this with the Axis2 Eclipse Plug-in and others do this from Ant.

I’m going to explain how I do this with Ant. You should know an aar file have a structure. Here is the structure like it’s explained on the Axis2 documentation.

DirectoryStructure.jpg

So How do you do this with Ant. You are a developer so you think like :

  1. first I need to recreate this structure
  2. then I need to package the whole thing into an aar.

So you are going to use the mkdir and copy Ant task to recreate a directory with an internal structure like it should be… WRONG!

First you are going to waist your memory because you need one archive and you are going to create one directory containing all the stuff and the archive. Second, you are going to waist you time because every time you will need to recreate you archive, you will need to delete the directory with all the crapy things and then copy all the compiled source code, all the libraries and you service.xml file inside the right structured directory before creating the jar. And this can take a while depending on the number of libraries, the size of the project,…

And then comes the zipfileset Ant task type. This task type behave like the fileset task (see Apache Ant Tasks Overview) but with an extra attribute called prefix. The prefix works like this :

<target name=“war” depends=“clean,compile”>

<jar destfile=“${jar.dir}/${ant.project.name}.war” basedir=“${classes.dir}”>

<fileset dir=“${bin}”>

<includes name=“**/*.class” />

</fileset>

</zipfileset dir=“${conf}” includes=“services.xml” prefix=“META-INF”>

</jar>

</target>

That’s it, no need to recreate the whole file and directory structure before creating the archive!