最終效果圖如:
<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+MS6000dpdMnPz8LU2CBjb2NvczJktcTRucv1sPwstPO4xTEwME08L3A+CjxwPjxpbWcgc3JjPQ=="/uploadfile/Collfiles/20140906/2014090608350613.png" alt="\">
2.解壓後,進入cocos2d主目錄,復制路徑到終端
3.執行./install.sh開始安裝(實質是拷貝至XCode目錄)
安裝前XCode,新建項目時界面如下
安裝後XCode,新建項目時界面如下
建好的工程目錄結構如下
直接運行程序,效果如下:
應用代理
// // AppDelegate.h // 31_cocos2D入門 // // Created by beyond on 14-9-5. // Copyright com.beyond 2014年. All rights reserved. // AppDelegate 繼承自 CCAppDelegate /* CCAppDelegate 繼承自NSObect,遵守協議:CCAppDelegate 擁有成員:UIWindow *window_ * 大多 Cocos2d 應用 應該重寫 CCAppDelegate, CCAppDelegate作為應用的程序入口. 至少應該復寫 startScene 方法,以便 返回應用要展示的首個場景 如果想更進一步定制 Cocos2d(例如自定義顯示的像素模式), 請復寫 applicaton:didFinishLaunchingWithOptions: 方法 */ #import "cocos2d.h" @interface AppDelegate : CCAppDelegate @end
// // AppDelegate.m // 31_cocos2D入門 // // Created by beyond on 14-9-5. // Copyright com.beyond 2014年. All rights reserved. // AppDelegate 繼承自 CCAppDelegate /* CCAppDelegate 繼承自NSObect,遵守協議:CCAppDelegate 擁有成員:UIWindow *window_ * 大多 Cocos2d 應用 應該重寫 CCAppDelegate, CCAppDelegate作為應用的程序入口. 至少應該復寫 startScene 方法,以便 返回應用要展示的首個場景 如果想更進一步定制 Cocos2d(例如自定義顯示的像素模式), 請復寫 applicaton:didFinishLaunchingWithOptions: 方法 */ #import "AppDelegate.h" #import "IntroScene.h" #import "HelloWorldScene.h" @implementation AppDelegate // -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 當繼承自CCAppDelegate,唯一要實現的方法就是這一個方法,應用首次啟動就會第一個執行本方法 // 在這兒,可以設置 Cocos2D 各參數的默認值 // There are a number of simple options you can change. // 如果你還嫌調用setupCocos2dWithOptions方法,不夠靈活自由,那麼你可以自己頂置 Cocos2D [self setupCocos2dWithOptions:@{ // 顯示 FPS CCSetupShowDebugStats: @(YES), // 使用降低了的幀率 // CCSetupAnimationInterval: @(1.0/30.0), // 使用一個加快的幀率 // CCSetupFixedUpdateInterval: @(1.0/180.0), // 設置屏幕為豎屏模式 // CCSetupScreenOrientation: CCScreenOrientationPortrait, // 使用16位顏色: // CCSetupPixelFormat: kEAGLColorFormatRGB565, // 使用一個統一的固定的坐標系統 // CCSetupScreenMode: CCScreenModeFixed, // Make iPad's act like they run at a 2x content scale. (iPad retina 4x) // CCSetupTabletScale2X: @(YES), // 更多內容請參閱 CCAppDelegate.h }]; return YES; } -(CCScene *)startScene { // 本方法返回應用啟動時,第一個要展示的場景 return [IntroScene scene]; } @end
// // IntroScene.h // 31_cocos2D入門 // // Created by beyond on 14-9-5. // Copyright com.beyond 2014年. All rights reserved. // // Importing cocos2d.h and cocos2d-ui.h, will import anything you need to start using cocos2d-v3 #import "cocos2d.h" #import "cocos2d-ui.h" /** * The intro scene * Note, that scenes should now be based on CCScene, and not CCLayer, as previous versions * Main usage for CCLayer now, is to make colored backgrounds (rectangles) * */ @interface IntroScene : CCScene + (IntroScene *)scene; - (id)init; @end
// // IntroScene.m // 31_cocos2D入門 // // Created by beyond on 14-9-5. // Copyright com.beyond 2014年. All rights reserved. // #import "IntroScene.h" #import "HelloWorldScene.h" @implementation IntroScene #pragma mark - 生命周期 + (IntroScene *)scene { return [[self alloc] init]; } - (id)init { self = [super init]; if (!self) return(nil); // 創建背景顏色為深灰色 CCNodeColor *background = [CCNodeColor nodeWithColor:[CCColor colorWithRed:0.2f green:0.2f blue:0.2f alpha:1.0f]]; [self addChild:background]; // 創建文字標簽 CCLabelTTF *label = [CCLabelTTF labelWithString:@"Hello Beyond" fontName:@"Chalkduster" fontSize:36.0f]; label.positionType = CCPositionTypeNormalized; label.color = [CCColor redColor]; // 屏幕的正中間 注意這裡是笛卡爾坐標系,原點在左下方 label.position = ccp(0.5f, 0.5f); [self addChild:label]; // 創建一個開始按鈕,點擊後進入下一個場景 CCButton *helloWorldButton = [CCButton buttonWithTitle:@"[ Start ]" fontName:@"Verdana-Bold" fontSize:18.0f]; helloWorldButton.positionType = CCPositionTypeNormalized; // 屏幕的中間靠下方 注意這裡是笛卡爾坐標系,原點在左下方 helloWorldButton.position = ccp(0.5f, 0.35f); // 監聽點擊事件 [helloWorldButton setTarget:self selector:@selector(onSpinningClicked:)]; [self addChild:helloWorldButton]; // 返回創建好的場景對象 return self; } #pragma mark - 按鈕點擊事件,切換至下一場景 - (void)onSpinningClicked:(id)sender { // 動畫切換至下一個場景 [[CCDirector sharedDirector] replaceScene:[HelloWorldScene scene] withTransition:[CCTransition transitionPushWithDirection:CCTransitionDirectionLeft duration:1.0f]]; } @end
// // HelloWorldScene.h // 31_cocos2D入門 // // Created by beyond on 14-9-5. // Copyright com.beyond 2014年. All rights reserved. // // Importing cocos2d.h and cocos2d-ui.h, will import anything you need to start using Cocos2D v3 #import "cocos2d.h" #import "cocos2d-ui.h" /** * 主場景 */ @interface HelloWorldScene : CCScene + (HelloWorldScene *)scene; - (id)init; @end
// // HelloWorldScene.m // 31_cocos2D入門 // // Created by beyond on 14-9-5. // Copyright com.beyond 2014年. All rights reserved. // #import "HelloWorldScene.h" #import "IntroScene.h" @implementation HelloWorldScene { CCSprite *_sprite; } #pragma mark - 生命周期 + (HelloWorldScene *)scene { return [[self alloc] init]; } - (id)init { if (!(self = [super init]) ) return(nil); // 場景模式下 允許交互 self.userInteractionEnabled = YES; // 創建背景顏色為深灰色 CCNodeColor *background = [CCNodeColor nodeWithColor:[CCColor colorWithRed:0.2f green:0.2f blue:0.2f alpha:1.0f]]; [self addChild:background]; // 添加一個精靈,並設置位置居中 _sprite = [CCSprite spriteWithImageNamed:@"ColorCircle.png"]; _sprite.position = ccp(self.contentSize.width/2,self.contentSize.height/2); [self addChild:_sprite]; // 為精靈創建一個旋轉動作,並且調用精靈的runAction方法,重復執行動作 CCActionRotateBy* actionSpin = [CCActionRotateBy actionWithDuration:1.5f angle:360]; [_sprite runAction:[CCActionRepeatForever actionWithAction:actionSpin]]; // 右上方,創建一個返回按鈕,點擊後,返回至上一個場景 CCButton *backButton = [CCButton buttonWithTitle:@"[ Back ]" fontName:@"Verdana-Bold" fontSize:18.0f]; backButton.positionType = CCPositionTypeNormalized; // 屏幕的右上方 注意這裡是笛卡爾坐標系,原點在左下方 backButton.position = ccp(0.85f, 0.95f); // 監聽點擊事件 [backButton setTarget:self selector:@selector(onBackClicked:)]; [self addChild:backButton]; // 返回創建好的場景對象 return self; } - (void)dealloc { // clean up code goes here } #pragma mark - Enter & Exit // ----------------------------------------------------------------------- - (void)onEnter { // 必須總是先調用父類的onEnter方法 [super onEnter]; // In pre-v3, touch enable and scheduleUpdate was called here // In v3, touch is enabled by setting userInteractionEnabled for the individual nodes // Per frame update is automatically enabled, if update is overridden } - (void)onExit { // 必須總是 最後才調用父類的onExit方法 [super onExit]; } #pragma mark - 用戶觸摸屏幕,精靈跟隨手指移動 -(void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event { CGPoint touchLoc = [touch locationInNode:self]; // 輸出觸摸點的坐標 CCLOG(@"Move sprite to @ %@",NSStringFromCGPoint(touchLoc)); // 移動精靈到觸摸點處 To表示絕對 By表示相對 CCActionMoveTo *actionMove = [CCActionMoveTo actionWithDuration:1.0f position:touchLoc]; // 調用精靈的runAction方法執行動作 [_sprite runAction:actionMove]; } #pragma mark - 按鈕點擊事件 - (void)onBackClicked:(id)sender { // 使用轉場動畫,切換場景至 IntroScene [[CCDirector sharedDirector] replaceScene:[IntroScene scene] withTransition:[CCTransition transitionPushWithDirection:CCTransitionDirectionRight duration:1.0f]]; } @end