學習cocos2d — 情境建立

來源:互聯網
上載者:User

在我們建立一個cocos-2d模板之後,系統自動產生一個情境Demo,運行結果顯示HelloWord


建立一個cocos2d ios工程

運行結果,我們點擊Achievments  和 LeaderBoard 會分別跳轉到其他情境

建立工程裡面目錄結構

上面的六個.h和.m檔案如果需要實現某功能是需要開發人員在裡面編輯代碼的,Resouces檔案夾用於存放圖片,音效檔文字檔案plist檔案等檔案,這樣組織便於我們進行管理工程裡面檔案;libs下的檔案就是cocos2d引擎了,存放的是源檔案,因為它屬於開源,所以可以看到他底層實現原理;還有常用的framework架構


產生的第一個HelloWord代碼實現全都在HelloWorldLayer.h HelloWorldLayer.h和HelloWorldLayer.m檔案中-(id)initf方法中,貼上他的原始碼

// on "init" you need to initialize your instance-(id) init{// always call "super" init// Apple recommends to re-assign "self" with the "super's" return valueif( (self=[super init]) ) {// create and initialize a LabelCCLabelTTF *label = [CCLabelTTF labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64];// ask director for the window sizeCGSize size = [[CCDirector sharedDirector] winSize];// position the label on the center of the screenlabel.position =  ccp( size.width /2 , size.height/2 );// add the label as a child to this Layer[self addChild: label];//// Leaderboards and Achievements//// Default font size will be 28 points.[CCMenuItemFont setFontSize:28];// Achievement Menu Item using blocksCCMenuItem *itemAchievement = [CCMenuItemFont itemWithString:@"Achievements" block:^(id sender) {GKAchievementViewController *achivementViewController = [[GKAchievementViewController alloc] init];achivementViewController.achievementDelegate = self;AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];[[app navController] presentModalViewController:achivementViewController animated:YES];[achivementViewController release];}   ];// Leaderboard Menu Item using blocksCCMenuItem *itemLeaderboard = [CCMenuItemFont itemWithString:@"Leaderboard" block:^(id sender) {GKLeaderboardViewController *leaderboardViewController = [[GKLeaderboardViewController alloc] init];leaderboardViewController.leaderboardDelegate = self;AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];[[app navController] presentModalViewController:leaderboardViewController animated:YES];[leaderboardViewController release];}   ];CCMenu *menu = [CCMenu menuWithItems:itemAchievement, itemLeaderboard, nil];[menu alignItemsHorizontallyWithPadding:20];[menu setPosition:ccp( size.width/2, size.height/2 - 50)];// Add the menu to the layer[self addChild:menu];}return self;}

剛開始接觸,裡面很多類都不知道,但是能看懂大部分代碼作用,工程裡面自動添加有句釋,又容易理解些

現在我們建立一個自己的情境,

在cocos-2d_2檔案下右鍵,選擇New File

    

出現CCNode Class ,

 

點擊Next,選擇Subclass of  CCLayer,在Next,建立檔案名稱為MyScenen

 

在Myscene.h裡面添加

+(CCScene *) scene;方法,在MyScene.m實現

+(CCScene *)scene{//    +(id)node 靜態初始化    CCScene *scene = [CCScene node];    MyScene *layer = [MyScene node];    //    把MyScene對象layer添加到CCScene對象中    [scene addChild:layer];    //    把CCScene對象返回給調用者    return scene;}

再到IntroLayer.h檔案中添加標頭檔,在IntrolLayer.m中最後面修改一個方法   -(void) makeTransition:(ccTime)dt  ,把HelloWorldLayer替換成MyScene

-(void) makeTransition:(ccTime)dt{[[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0 scene:[MyScene scene] withColor:ccWHITE]];}

此時我們可以刪除HelloWirldLayer檔案運行結果是一個黑屏,因為在情境中我們沒有添加任何東西,

現在在情境上添加一個Label

-(id) init{if( (self=[super init]) ) {// 建立一個標籤,設定字型樣式和大小CCLabelTTF *label = [CCLabelTTF labelWithString:@"建立一個標籤" fontName:@"Marker Felt" fontSize:64];        // 擷取主視窗大小CGSize size = [[CCDirector sharedDirector] winSize];        // 設定標籤的情境位置label.position =  ccp( size.width /2 , size.height/2 );// 把標籤添加到這個[self addChild: label];            }        return  self;}

運行效果



///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

現在在讓這個Label有點動態效果,當點擊螢幕時候切換字型大小

在-(id)init方法中添加兩行代碼

-(id) init{if( (self=[super init]) ) {// 建立一個標籤,設定字型樣式和大小CCLabelTTF *label = [CCLabelTTF labelWithString:@"建立一個標籤" fontName:@"Marker Felt" fontSize:64];        // 擷取主視窗大小CGSize size = [[CCDirector sharedDirector] winSize];        // 設定標籤的情境位置label.position =  ccp( size.width /2 , size.height/2 );// 把標籤添加到這個[self addChild: label];                //        設定標籤Tag屬性,數字可以隨便設,但是要是唯一的        label.tag = 8;//        添加觸摸處理事件        self.isTouchEnabled = YES;    }        return  self;}

事件方法

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{//    通過tag屬性訪問Label標籤    CCNode *node = [self getChildByTag:8];//    驗證返回對象是否一致    NSAssert([node isKindOfClass:[CCLabelTTF class]],@"node is not a CCLabelTTF");//    類型轉換    CCLabelTTF *label = (CCLabelTTF *)node;    //    CCRANDOM_0_1();是一個宏定義,用來改變label的scale熟悉    label.scale = CCRANDOM_0_1();}

看一下運行效果,字型大小都是隨機的

原始碼:http://download.csdn.net/detail/duxinfeng2010/4592145

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.