Feel free to fix/add documentation to the wiki
Hello World
Valid for cocos2d v0.99.0 or newer
This example ONLY shows the basic steps to render a label on the screen. Some advanced options regarding the initialization are not present just to simplify the example. Once you understand this example, you can read the hello_actions sample.
Implementation File
File: HelloWorld.m
Import headers
Like in c / c++ / etc, you need to import some headers.
// Needed for UIWindow, NSAutoReleasePool, and other objects #import <UIKit/UIKit.h> // Import the interfaces #import "HelloWorld.h"
Layer
The HelloWorld implementation. This is where the magic happens.
// HelloWorld implementation @implementation HelloWorld // on "init" you need to initialize your instance -(id) init { // always call "super" init // Apple recommends to re-assign "self" with the "super" return value if( (self=[super init] )) { // create and initialize a Label CCLabel* label = [CCLabel labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64]; // ask director the the window size CGSize size = [[CCDirector sharedDirector] winSize]; // position the label on the center of the screen label.position = ccp( size.width /2 , size.height/2 ); // add the label as a child to this Layer [self addChild: label]; } return self; } // on "dealloc" you need to release all your retained objects - (void) dealloc { // in case you have something to dealloc, do it in this method // in this particular example nothing needs to be released. // cocos2d will automatically release all the children (Label) // don't forget to call "super dealloc" [super dealloc]; } @end
Application Delegate
This is the Application delegate implementation. Probably all your games will have a similar Application Delegate. For the moment it's not that important if you don't understand the following code.
// // Application Delegate implementation. // Probably all your games will have a similar Application Delegate. // For the moment it's not that important if you don't understand the following code. // @implementation AppController // window is a property. @synthesize will create the accesors methods @synthesize window; // Application entry point - (void) applicationDidFinishLaunching:(UIApplication*)application { // Try to use CADisplayLink director // if it fails (SDK < 3.1) use NSTimer (default) director if( ! [CCDirector setDirectorType:CCDirectorTypeDisplayLink] ) [CCDirector setDirectorType:CCDirectorTypeDefault]; // create an initilize the main UIWindow window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Attach cocos2d to the window [[CCDirector sharedDirector] attachInWindow:window]; // before creating any layer, set the landscape mode [[CCDirector sharedDirector] setDeviceOrientation:CCDeviceOrientationLandscapeLeft]; // Make the window visible [window makeKeyAndVisible]; // Create and initialize parent and empty Scene CCScene *scene = [CCScene node]; // Create and initialize our HelloWorld Layer CCLayer *layer = [HelloWorld node]; // add our HelloWorld Layer as a child of the main scene [scene addChild:layer]; // Run! [[CCDirector sharedDirector] runWithScene: scene]; } - (void) dealloc { [window release]; [super dealloc]; } @end
Main Entry Point
Since objective-c is also a c / c++ language, the main entry point is still main
int main(int argc, char *argv[]) { // it is safe to leave these lines as they are. NSAutoreleasePool *pool = [NSAutoreleasePool new]; UIApplicationMain(argc, argv, nil, @"AppController"); [pool release]; return 0; }
Header File
And this is the header file:
File: HelloWorld.h
// When you import this file, you import all the cocos2d classes #import "cocos2d.h" // Application Delegate class @interface AppController : NSObject <UIAccelerometerDelegate, UIAlertViewDelegate, UITextFieldDelegate, UIApplicationDelegate> { // main UIWindow // The OpenGL view will be a attached to this UIWindow UIWindow *window; } // Make the main UIWindow a property @property (nonatomic, retain) UIWindow *window; @end // HelloWorld Layer @interface HelloWorld : CCLayer { } @end
Trace: » cocos2d_and_uikitlayer » tiled_maps » hello_actions » hello_world
