zoukankan      html  css  js  c++  java
  • 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

    版权声明:本文博主原创文章。博客,未经同意不得转载。

  • 相关阅读:
    虚拟机与宿主机网络配置——可互通可上网
    CentOs7下安装Redis
    RabbitMQ使用(下)
    RabbitMQ使用(上)
    定制属于你自己的ViewEngine(一套逻辑多套UI)
    JWT With NetCore WebApi
    day02
    day01
    4、python与MongoDB交互
    3、MongoDB的详细查找
  • 原文地址:https://www.cnblogs.com/yxwkf/p/4803086.html
Copyright © 2011-2022 走看看