cocos2d第一天-熟悉cocos2d

cocos2d 是ios最經常使用到的遊戲底層框架,有於底層也是使用objective-c編寫,因此速度上和代碼書寫上要比其餘框架好寫,好熟悉的多。node

好了,廢話很少說,開始:ios

  先認識兩個最基礎的概念。objective-c

CCScene 類是一個抽象概念,它僅用於根據對象的像素座標把對象放置到場景中正確
的位置。因此,CCScene 節點一般是整個 cocos2d 場景體系的根節點。多數時候只有一個
運行着的場景,不過從一個場景過渡到另外一個場景的狀況是例外。
CCLayer 類自己幾乎沒有什麼功能,但它可用於接收觸摸和加速計輸入。它常被用做
CCScene 的第一個子節點,由於每一個遊戲至少都會使用觸摸輸入的功能。
 
一般最初先新建一個場景scene,場景能夠簡單的理解爲一個遊戲的一關,一關一個場景。

因爲 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。下次再追加,先寫這麼多吧!
相關文章
相關標籤/搜索