I finally got around to downloading the iPhone SDK to see what I can do with it. Since I’ve dabbled in game development back when I worked with Flash and Director, I figured I’d see what I can do with the iPhone. I wanted to start with a framework and I looked at Torque and Unity. Though inexpensive by game engine standards, during a recessionary time of cost cutting, it’s hard to validate a nearly $1000 purchase, especially if you don’t know yet if that investment would pay off. So I finally settled on the cocos2d-iphone framework which is based on the cocos2d framework for Python. There is little to no documentation, so be prepared to read source code. Monocle Studio’s whitepaper will definitely help you bootstrap your project. In the spirit of that paper, I thought I’d share my translation of the cocos2d “Hello, World” example adapted for the cocos2d-iphone framework.
First step is to setup your project according to Monocle Studio’s whitepaper through “Cleaning up the project” right before “Creating a main menu”. That’ll create the base project. You’ll probably want to change the name to something else (e.g. CocosHelloWorld) instead of SimpleGame. Make sure to change the name where appropriate, for example SimpleGameAppDelegate should be YourAppNameAppDelegate.
Create the HelloWorld class
- Click the Classes group in Groups & Files pane.
- Click the Action button
- Select Add->New File…
- Choose NSObject subclass
- Name the class “HelloWorld”
Open HelloWorld.h and change the code to look like the following. This says that the HelloWorld class is derived from the Layer class (which is derived from the CocosNode class).
#import <UIKit/UIKit.h> #import "cocos2d.h" @interface HelloWorld : Layer { } @end
Open HelloWorld.m and change the code to the following. This says that the HelloWorld class uses its parent class for initialization. If the initialization is successful, then create a new label, place it in the center, and add it to the layer. Finally, it returns itself.
#import "HelloWorld.h" @implementation HelloWorld - (id) init { self = [super init]; if (self != nil) { Label *label = [Label labelWithString:@"Hello, World!" fontName:@"Times New Roman" fontSize:32]; label.position = cpv(240,160); [self add:label]; } return self; } @end
Setup the Director
Open up YourAppNameAppDelegate.h and change the include declarations to:
#import <UIKit/UIKit.h> #import "cocos2d.h" #import "HelloWorld.h"
Next open YourAppNameAppDelegate.m and change it to the following. The difference between this and the corresponding method in SimpleGame are the last two lines. The first creates a Scene node and adds a HelloWorld node to the scene. The next line starts the Director using the just created Scene as the initial scene. The previous set of lines prepare the iPhone display by initializing a window, enabling the UI for the window, setting the Director to display in landscape mode, then attach the Director to the window, and finally making the window the main window and turning it on.
#import "CocosHelloWorldAppDelegate.h" @implementation CocosHelloWorldAppDelegate - (void)applicationDidFinishLaunching:(UIApplication *)application { UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; [window setUserInteractionEnabled:YES]; [window setMultipleTouchEnabled:YES]; [[Director sharedDirector] setLandscape: YES]; [[Director sharedDirector] attachInWindow:window]; [window makeKeyAndVisible]; Scene *s = [[Scene node] add:[HelloWorld node]]; [[Director sharedDirector] runWithScene:s]; } @end
If all is well, if you click Build & Go the iPhone simulator should display “Hello, World!”.