iOS_31_cocos2d環境搭建

來源:互聯網
上載者:User

標籤:ios   cocos2d   動畫   遊戲   

最終如:




1.從Git上下載 cocos2d的壓縮包,大概100M


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,遵守協議:<UIApplicationDelegate, CCDirectorDelegate>    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,遵守協議:<UIApplicationDelegate, CCDirectorDelegate> 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:@{// 顯示 FPSCCSetupShowDebugStats: @(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










iOS_31_cocos2d環境搭建

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.