Cocos2d-iPhone V3 (1) 基本程序框架以及常用動作介紹

Cocos2d-iPhone V3 (1) 基本程序框架以及常用動作介紹

  • 博客:http://blog.csdn.net/prevention
  • 作者:大銳哥

-

第一部分:一個 Cocos2d-iPhone V3 的基本框架

1. AppDelegate

首先看AppDelegate.h,類是繼承自CCAppDeleagate,其他沒什麼特別的:

#import "cocos2d.h"
@interface AppDelegate : CCAppDelegate
@end

再看AppDelegate.m,只要實現兩個函數即可,注意其中實現startScene就可以加載你自定義的場景啦,簡單吧:

#import "AppDelegate.h"
#import "MainScene.h"

@implementation AppDelegate

-(BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self setupCocos2dWithOptions:@{
        CCSetupShowDebugStats: @(YES),
    }];
    return YES;
}

- (CCScene *)startScene
{
    return [HomeScene scene];
}

2. 你自己的場景類 MainScene

先看看MainScene.h,我們看有兩個方法,一個是靜態方法scene,一個是類方法init

#import "cocos2d.h"
#import "cocos2d-ui.h"

@interface MainScene : CCScene

+ (MainScene *)scene;
- (id)init;

@end

再看看MainScene.m,這裏頭東西就多了。首先看整體結構:

#import "MainScene.h"

@implementation MainScene
{
    CCSprite *_sprite;
}

+ (MainScene *)scene { /* ... */ }
- (id)init { /* ... */ }
- (void)dealloc { /* ... */ }
- (void)onEnter { /* ... */ }
- (void)onExit { /* ... */ }
- (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event { /* ... */ }

@end

必須要有一個靜態方法返回一個自定義的場景實例scene方法。還要有onEnteronExit表示進入/離開該場景就會調用。touchBegan是一個 Touch Handler。

2.1. 靜態方法 scene

沒什麼好說的:

+ (MainScene *)scene
{
    return [[self alloc] init];
}

2.2. init

- (id)init
{
    // Apple recommend assigning self with supers return value
    self = [super init];
    if (!self) return(nil);

    // Enable touch handling on scene node
    self.userInteractionEnabled = YES;

    // Create a colored background (Dark Grey)
    CCNodeColor *background =
        [CCNodeColor nodeWithColor:[CCColor colorWithRed:0.2f
                                                   green:0.2f
                                                    blue:0.2f
                                                   alpha:1.0f]];
    [self addChild:background];

    // Add a sprite
    _sprite = [CCSprite spriteWithImageNamed:@"Icon-72.png"];
    _sprite.position  = ccp(self.contentSize.width/2,self.contentSize.height/2);
    [self addChild:_sprite];

    // Animate sprite with action
    CCActionRotateBy* actionSpin = [CCActionRotateBy actionWithDuration:1.5f angle:360];
    [_sprite runAction:[CCActionRepeatForever actionWithAction:actionSpin]];


    // done
    return self;
}
  • 調用superinit
  • 設置userInteractionEnabledYES來接收觸摸事件
  • 添加背景節點,這裏用的是CCNodeColor
  • 添加精靈節點CCSprite
  • 給精靈節點添加動作CCActionRotateBy
  • 返回self

2.3. 進入場景 Handler:onEnter

一定要記得調用superonEnter

- (void)onEnter
{
    [super onEnter];
}

2.4. 離開場景 Handler:onExit

一定要記得調用superonExit

- (void)onExit
{
    [super onExit];
}

2.5. Touch Handler

- (void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
    CGPoint touchLoc = [touch locationInNode:self];
    CCActionMoveTo *actionMove =
        [CCActionMoveTo actionWithDuration:1.0f position:touchLoc];
    [_sprite runAction:actionMove];
}
  • 首先根據傳入的UITouch參數來獲取被觸摸位置CGPoint
  • 根據獲取到的位置設定CCAction,最後運行這個CCAction

第二部分:動作

1. 位移一段距離CCActionMoveBy

+ (id)actionWithDuration:(CCTime)duration position:(CGPoint)deltaPosition;

2. 位移到CCActionMoveTo

+ (id)actionWithDuration:(CCTime)duration position:(CGPoint)position;

3. 旋轉一個角度CCActionRotateBy

注意其中的 angle 是角度(一週 360 度),不是弧度(一週 2π):

+ (id)actionWithDuration:(CCTime)duration angle:(float)deltaAngle;

4. 旋轉到CCActionRotateTo

注意其中的 angle 是角度(一週 360 度),不是弧度(一週 2π):

+ (id)actionWithDuration:(CCTime)duration angle:(float)angle;

5. 漸變出現CCActionFadeIn

This action fades in the target, it modifies the opacity from 0 to 1.

+ (id)actionWithDuration:(CCTime)d;

6. 漸變消失CCActionFadeOut

This action fades out the target, it modifies the opacity from 1 to 0.

+ (id)actionWithDuration:(CCTime)d;

7. 漸變到CCActionFadeTo

你可能會注意到 Cocos2d 的源碼裏有拼寫錯誤,opacity寫成了opactiyCCActionInterval.h中):

/**
 *  Creates a fade action.
 *
 *  @param duration Action duration.
 *  @param opactiy  Opacity to fade to.
 *
 *  @return New fade action.
 */
+ (id)actionWithDuration:(CCTime)duration opacity:(CGFloat)opactiy;

-

轉載請註明來自:http://blog.csdn.net/prevention

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章