cocos2d 是ios最經常使用到的遊戲底層框架,有於底層也是使用objective-c編寫,因此速度上和代碼書寫上要比其餘框架好寫,好熟悉的多。node
好了,廢話很少說,開始:ios
先認識兩個最基礎的概念。objective-c
因爲 CCScene 類是抽象概念,所以建立場景的默認方式是經過類中的靜態初始化方法
「+(id) scene」來完成的。該方法建立了一個普通的 CCScene 對象框架
layer是場景中一個精靈或者說一個動做的載體。字體
+(id) scene { CCScene *scene = [CCScene node]; id layer = [HelloWorldLayer node]; [scene addChild:layer]; return scene; }
有了場景就開始建立動做的載體layerthis
在layer初始化的時候,添加必要的載體spa
-(id) init { if ((self = [super init])) { // create and initialize a label CCLabelTTF* label = [CCLabelTTF labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64]; // get the window (screen) size from CCDirector CGSize size = [[CCDirector sharedDirector] winSize]; // position the label at the center of the screen label.position = CGPointMake(size.width / 2, size.height / 2); // add the label as a child to this Layer [self addChild:label]; } return self; }
添加一個字體顯示code
Hello World,字體爲 Marker Felt,Marker Felt這個字體爲cocos提供的默認字體。這樣在頁面就會顯示一個 Hello World。下次再追加,先寫這麼多吧!