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

你可能感兴趣的:(Cocos2d-iPhone V3 (1) 基本程序框架以及常用动作介绍)