התלבטתי קלות מה לרשום בפוסט הראשון ואז ראיתי את הכותרת של הפוסט שמגיע עם התבנית של וורדפרס: Hello world!, אז קבלו את "שלום עולם" של cocos2d:

בכל class ב-Objective C יש את קבצי ה-h וה-m, ה-Header וה-Implementation. נתחיל עם קובץ ה-h של HelloWorldScene class.
בקבצי ה-h מגדירים את ה-class: משתנים, פונקציות ו-properties (קיצורי דרך לפונ' set ו-get). הקובץ HelloWorldScene.h הוא דיי פשוט, יש רק הגדרה של פונ' (או Method) אחת ליצירת הסצנה - scene.
אפליקציה ב-cocos2d מורכבת מסצנות (scenes), והסצנות בנויות משכבות (Layers). למעשה, ניתן להגדיר סצנה ע"י class אשר יורש מ-CCLayer (שמות כל ה-classes ב-cocos2d מתחילים ב-CC).
// When you import this file, you import all the cocos2d classes
#import "cocos2d.h"
// HelloWorld Layer
@interface HelloWorld : CCLayer
{
}
// returns a Scene that contains the HelloWorld as the only child
+(id) scene;
@end
נעבור לקובץ HelloWorldScene.m. בתחילתו נמצא, כמו בכל קובץ המגדיר סצנה, את יישום הפונ' שיוצרת את הסצנה ואת השכבה, ומשייכת את השכבה לסצנה. במקרה שלנו, שכבת HelloWorld.
// Import the interfaces
#import "HelloWorldScene.h"
// HelloWorld implementation
@implementation HelloWorld
+(id) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
HelloWorld *layer = [HelloWorld node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
הפונ' הבאה בקובץ- 'init', היא הפונ' שנקראת בתחילת כל סצנה ובה מאתחלים את הסצנה.
// 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
CCLabelTTF *label = [CCLabelTTF labelWithString:@"Hello cocos2d" 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;
}
לסיום, נכתוב את הפונ' dealloc, בה משחררים אובייקטים, אך בדוגמה הנ"ל לא הכרזנו על אובייקטים שצריך לשחרר.
// 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