通常调用某个动作的方法: 

// 创建动作id actionTo = [CCMoveTo actionWithDuration: 2 position:ccp(s.width-40, s.height-40)];// 使用动作。(说明:tamara 是一个 CCSprite。)
[tamara runAction: actionTo];


瞬时动作

顾名思义。瞬时动作就是不需要时间,马上就完成的动作。瞬时动作的共同基类是 InstantAction。

放置 – Place 

效果类似于 node.Position = ccp(x, y)。之所以作为一个动作来实现是为了可以与其他动作形成一个连续动作。

示例:

- (void) OnPlaceMenue:(id) sender {
CGSize s = [[CCDirector sharedDirector] winSize];
CGPoint p = ccp(CCRANDOM_0_1() * s.width, CCRANDOM_0_1() * s.height);
[sprite runAction:[CCPlace actionWithPosition:p]];
}

 隐藏 – Hide

效果类似于 [node setVisible:NO].

示例:

- (void) OnHideMenue:(id) sender {
[sprite runAction:[CCHide action]];
}

显示 – Show

效果类似于 [node setVisible:YES].

示例:

- (void) OnShowMenue:(id) sender {
[sprite runAction:[CCShow action]];
}

可见切换 – ToggleVisibility

代码如下:

- (void) OnToggleMenue:(id) sender {
[sprite runAction:[CCToggleVisibility action]];
}

延时动作

延时动作就是动作的完成需要一定时间。因此,actionWithDuration 是延时动作执行时的第一个参数,延时动作的共同基类是 CCIntervalAction。

函数的命名规则:

***To: 意味着运动到指定的位置。

***By:意味着运动到按照指定癿 x、y 增量的位置。(x、y 可以是负值)

移动到 – CCMoveTo 

移动– CCMoveBy 

跳跃到 – CCJumpTo

设置终点位置和跳跃癿高度和次数。

跳跃 – CCJumpBy

设置终点位置和跳跃癿高度和次数。

贝塞尔 – CCBezierBy

支持 3 次贝塞尔曲线:P0-起点,P1-起点切线方向,P2-终点切线方向,P3-终点。

首先设置定 Bezier 参数,然后执行。

放大到 – CCScaleTo

设置放大倍数,是浮点型。

放大 – CCScaleBy 

旋转到 – CCRotateTo 

旋转 – CCRotateBy 

闪烁 – CCBlink

设定闪烁次数

色调变化到 – CCTintTo 

色调变换 – CCTintBy 

变暗到 – CCFadeTo 

由无变亮 – CCFadeIn

由亮变无 – CCFadeOut


组合动作

按照一定的次序将上述基本动作组合起来,形成连贯的一套组合动作。组合动作包括以 下几类:

序列 – CCSequence

主要作用就是线序排列若干个动作,然后按先后次序逐个执行。

- (void) OnSequence:(id) sender {
CGSize s = [[CCDirector sharedDirector] winSize];
CGPoint p = ccp(s.width/2, 50);
// 创建5个动作
id ac0 = [sprite runAction:[CCPlace actionWithPosition:p]];
id ac1 = [CCMoveTo actionWithDuration:2 position:ccp(s.width - 50, s.height - 50)];
id ac2 = [CCJumpTo actionWithDuration:2 position:ccp(150, 50) height:30 jumps:5];
id ac3 = [CCBlink actionWithDuration:2 blinks:3];
id ac4 = [CCTintBy actionWithDuration:0.5 red:0 green:255 blue:255];
//将 5 个动作组合为一个序列,注意不要忘了用 nil 结尾。
[sprite runAction:[CCSequence actions:ac0, ac1, ac2, ac3, ac4, ac0, nil]];
}

同步 – Spawn Spawn

作用就是同时并列执行若干个动作,但要求动作都必须是可以同时执行的。比如:移动式翻转、变色、变大小等。

需要特别注意的是,同步执行最后的完成时间由基本动作中用时最大者决定。

- (void) OnSpawn:(id) sender {
CGSize s = [[CCDirector sharedDirector] winSize];
CGPoint p = ccp(s.width/2, 50);
sprite.rotation = 0;
[sprite setPosition:p];
// 创建 4 个需要并行的动作,确保动作用时可组合。2 – 2 - (1+1)
id ac1 = [CCMoveTo actionWithDuration:2 position:ccp(s.width - 50, s.height - 50)];
id ac2 = [CCRotateTo actionWithDuration:2 angle:180];
id ac3 = [CCScaleTo actionWithDuration:1 scale:4];
id ac4 = [CCScaleBy actionWithDuration:1 scale:0.5];
id seq = [CCSequence actions:ac3, ac4, nil];
// 同步动作和组合动作以形成一个连续的新动作。
[sprite runAction:[CCSpawn actions:ac1, ac2, seq, nil]];
}

重复有限次数 – Repeate

重复有限的次数的动作示例代码如下:

- (void) OnRepeat:(id) sender {
CGSize s = [[CCDirector sharedDirector] winSize];
CGPoint p = ccp(s.width/2, 50);
sprite.rotation = 0;
[sprite setPosition:p];
// 创建动作序列
id ac1 = [CCMoveTo actionWithDuration:2 position:ccp(s.width - 50, s.height - 50)];
id ac2 = [CCJumpBy actionWithDuration:2 position:ccp(-400, -200) height:30 jumps:5];
id ac3 = [CCJumpBy actionWithDuration:2 position:ccp(s.width/2, 0) height:20 jumps:3];
id seq = [CCSequence actions:ac1, ac2, ac3, nil];
//重复运行上述劢作序列 3 次。
[sprite runAction:[CCRepeat actionWithAction:seq times:3]];
}

反动作 – Reverse

反动作就是反向(逆向)执行某个动作,支持针对动作序列的反动作序列。反动作不是一个专门的类,而是 CCFiniteAction 引入的一个接口。不是所有的类都支持反动作,XxxxTo 类通常不支持反动作,XxxxBy 类通常支持。示例如下:

- (void) OnReverse:(id) sender {
CGSize s = [[CCDirector sharedDirector] winSize];
CGPoint p = ccp(s.width/2, 50);
sprite.rotation = 0;
[sprite setPosition:p];
id ac1 = [CCMoveBy actionWithDuration:2 position:ccp(190, 220)];
// 创建某个劢作癿反劢作。
id ac2 = [ac1 reverse];
[sprite runAction:[CCRepeat actionWithAction:[CCSequence actions:ac1, ac2,nil] times:2]];
}

动画 – Animation

动画就是让精灵自身的连续执行一段影像,形成模拟运动的效果:行走时动精灵状态、打斗时的状态等。

- (void) OnAnimation:(id) sender {
CCAnimation *animation = [AtlasAnimation animationWithName:@"flight" delay:0.2f];
// 每帧的内容定义。
for(int i=0;i<3;i++) {
int x= i % 3;
[animation addFrameWithRect: CGRectMake(x*32, 0, 31,30) ];
}
// 执行劢画效果
id action = [CCAnimate actionWithAnimation: animation];
[sprite runAction:[CCRepeat actionWithAction:action times:10]];
}

无限重复 – RepeatForever 

RepeatForever 是从 Action 类直接派生的,因此无法参于序列和同步;自身也无法反向执行。该类的作用就是无限期执行某个动作或动作序列,直到被停止。

- (void) OnRepeatForever:(id) sender {
CGSize s = [[Director sharedDirector] winSize];
CGPoint p = ccp(100, 50);
// 飞行喷火模拟劢画
CCAnimation *animation = [CCAnimation animationWithName:@"flight" delay:0.1f];
for(int i=0;i<3;i++)
{int x= i % 3;
[animation addFrameWithRect: CGRectMake(x*32, 0, 31,30) ];
}
id action = [CCAnimate actionWithAnimation: animation];
// 将该动画作为精灵的本征动画,一直运行。
[sprite runAction:[RepeatForever actionWithAction:action]];
// 在创建第二个连续无限期动作序列。叠加二者形成完整效果。
ccBezierConfig bezier;
sprite.rotation = 0;
[sprite setPosition:p];
bezier.startPosition = ccp(0,0);
bezier.controlPoint_1 = ccp(0, s.height/2);
bezier.controlPoint_2 = ccp(300, -s.height/2);
bezier.endPosition = ccp(300,100);
id ac10 = [CCBezierBy actionWithDuration:3 bezier:bezier];
id ac11 = [CCTintBy actionWithDuration:0.5 red:0 green:255 blue:255];
id ac1 = [CCSpawn actions:ac10, [Repeat actionWithAction:ac11 times:4], nil];
id ac2 = [CCSpawn actions:[ac10 reverse], [CCRepeat actionWithAction:ac11 times:4], nil];
// 第二个无限期连续运劢。
[sprite runAction:[CCRepeatForever actionWithAction:[CCSequence actions:ac1, ac2,nil]]];
}

速度变化

基本动作和组合动作实现了针对精灵的各种运动、动画效果的改变,但这样的改变的速度是不变的,通过 CCEaseAction 为基类的类系和 CCSpeed 类我们可以很方便的修改精灵执行动作的速度:由快至慢还是由慢至快

EaseIn 由慢至快。

EaseOut 由快至慢

EaseInOut 由慢至快再由快至慢。

EaseSineIn 由慢至快

EaseSineOut 由快至慢

EaseSineInOut 由慢至快再由快至慢。

EaseExponentialIn 由慢至极快。

EaseExponentialOut 由极快至慢。

EaseExponentialInOut 由慢至极快再由极快至慢。

Speed 人工设定速度,还可通过 SetSpeed 不断调整。

扩展动作

我们已经掌握了执行各种各样的动作,也可以按照不同的快慢修改动作执行的时间, Cocos2D-iPhone 还提供了针对现有动作的扩展,以实现各种灵活的效果。

延时动作 – Delay 

在动作序列中增加一个时间间歇:

- (void) OnDelay:(id) sender {
id ac1 = [CCMoveBy actionWithDuration:2 position:ccp(200, 200)];
id ac2 = [ac1 reverse];
// 实现一个等待间歇
[spriterunAction:[Sequenceactions:ac1,[DelayTime actionWithDuration:1], ac2, nil]];
}

函数调用  函数

在动作序列中间或者结束调用某个函数,执行任何需要执行的任务:动作、状态修改等。代码如下:

- (void) OnCallFunc:(id) sender {
id ac1 = [CCMoveBy actionWithDuration:2 position:ccp(200, 200)];
id ac2 = [ac1 reverse];
id acf = [CCCallFunc actionWithTarget:self selector:@selector(CallBack1)];
[sprite runAction:[CCSequence actions:ac1, acf, ac2, nil]];
}

对应的函数为:(再做一个动作,这就实现了动作、动作序列的任意扩展和连接)

- (void) CallBack1 {
[sprite runAction:[CCTintBy actionWithDuration:0.5 red:255 green:0 blue:255]];
}

带对象参数 调用自定义函数时,传递当前对象。

- (void) OnCallFuncN:(id) sender {
id ac1 = [CCMoveBy actionWithDuration:2 position:ccp(200, 200)];
id ac2 = [ac1 reverse];
id acf = [CallFuncN actionWithTarget:self selector:@selector(CallBack2:)];
[sprite runAction:[CCSequence actions:ac1, acf, ac2, nil]];
}

对应的自定义函数:(这里,我们直接使用了该对象)

- (void) CallBack2:(id)sender {
[sender runAction:[CCTintBy actionWithDuration:1 red:255 green:0 blue:255]];
}

带对象、数据参数调用自定义函数时,传递当前对象和一个常量(也可以是指针)。

- (void) OnCallFuncND:(id) sender {
id ac1 = [CCMoveBy actionWithDuration:2 position:ccp(200, 200)];
id ac2 = [ac1 reverse];
id acf = [CCCallFuncND actionWithTarget:self selector:@selector(CallBack3:data:) data:(void*)2];
[sprite runAction:[CCSequence actions:ac1, acf, ac2, nil]];
}

对应的自定义函数,我们使用了传递的对象和数据:

-(void) CallBack3:(id)sender data:(void*)data {
[sender runAction:[CCTintBy actionWithDuration:(NSInteger)data red:255 green:0 blue:255]]; }

cocos2d笔记 (4)cocos2d里的各种动作相关推荐

  1. ROS入门笔记(十二):动作编程 (C++)

    ROS入门笔记(十二):动作编程 (C++) 文章目录 01 导读 02 功能包的创建 03 在功能包中创建action(动作) 3.1 自定义action 3.2 在package.xml中添加功能 ...

  2. YOLOV3学习笔记(噼里啪啦up主)

    YOLOV3学习笔记(噼里啪啦up主) YOLOV3上面的IOU LOSS是错误的,因为在两个候选框和目标框围成的面积相同时,YOLOV3并没有告诉网络应该选哪个候选框. 2. 在YOLOV3中判断候 ...

  3. 【人工智能学习笔记】人工智能里的数学——概述

    系列文章目录 [人工智能学习笔记]人工智能里的数学--概述 [人工智能里的数学]一元函数微分学 [人工智能里的数学]线性代数基础 [人工智能里的数学]多元函数微分学 前言 与软件开发相比,人工智能领域 ...

  4. 读书笔记 | 之Photoshop里Pantone色类型该如何选择

    读书笔记 | 之Photoshop里Pantone色类型该如何选择 工作内容因为涉及到专色印刷,需考虑将RGB/CMYK模式下的色值转换为PANTONE专色值.PS的拾色器里有一个颜色库,可供不同的色 ...

  5. cocos2d笔记——解析HelloWorldScene

    HelloWorldScene类是纯coocs2d代码显示Hello World标签的地方.在开始深入这个之前,你要先明白cocos2d使用了一个CCNode对象层级来确定在什么地方显示什么东西.所有 ...

  6. cocos2d笔记——CCNode与CCAction

    CCNode CCNode是所有node的基类,它是一个抽象类,没有可视化表示形式,定义了所有node共有的属性和方法. 操作子节点的一些方法: CCNode* childNode = [CCNode ...

  7. 【Cocos2d开发】Cocos2d下安卓环境的搭建

    在进行Cocos2d游戏开发前 我们先来配置一下环境,我们先来准备一下工具,我们所需要的工具分别为: 1.Cocos2d引擎 2.JDK 3.SDK 4.NDK 5.ANT 6.ADT 1.下载Coc ...

  8. cocos2d for android,cocos2d jsb 打包 Android APK

    1.首先要会普通的cpp 打包成Android APK 以下所说的是在cocos2d-x 2.2.2 或者 2.3 版本中.本文在Eclipse总用ndk编译cocos2d-x. 老生常谈cocos2 ...

  9. 大一上学期计算机考试笔记,上大一波学霸笔记曝光 笔记本里的青春 有着努力的痕迹...

    "好记性不如烂笔头".很多学习都是需要记笔记的.好的笔记能使学习效率提高. 近日,上海大学征集了部分同学的课程笔记,其中既有大一社区学院新生,也有已被推免到名校的学霸,他们纷纷晒出 ...

最新文章

  1. addressof表达式不能转换为long_基本数据类型转换
  2. uwsgi gevent
  3. 关于Redis的数据迁移(三种方法)
  4. java ldap 连接池_使用Ldap连接池
  5. 电脑数据存储工具----光盘驱动器
  6. as3数据类型检查写法(is/as/typeof/in/instanceof)用法介绍
  7. 06 iOS 关闭侧滑返回
  8. 一文搞定Redis五大数据类型及使用场景
  9. 2507-AOP- springboot中使用-使用注解方式
  10. 使用Origin绘制折线图(入门)
  11. 联想启天m428进入bios安装ubuntu
  12. 进销存小程序(一)项目整体功能和设计
  13. winwodws下c语言延时函数,lex实验报告(共10篇).doc
  14. 创业指南:如何快速拿到天使投资?
  15. NOIP2017 普及 luogu3957 跳房子
  16. 一起来做NES开发(1)
  17. 利用手机软件fing查询树莓派的ip地址
  18. c++Primer5,总览与IO库和泛型算法
  19. 2017-2018-1 20155323 《信息安全系统设计基础》第十三周学习总结
  20. 国产汽车级可编程线性霍尔传感器CHA611,可以替代Allegro的A1363系列产品,解决汽车级芯片缺货难题

热门文章

  1. Shp数据批量导入Postgresql工具的原理和设计
  2. 游戏服务器数据库踩过的坑
  3. Iphone获取本地ip地址
  4. 【仿某公司前台】 asp安全查询系统
  5. Java编译带包文件
  6. 454. 四数相加 ||
  7. 车联网中如何应用大数据
  8. 可视化数据图表制作注意事项
  9. 物联网卡和车联网有什么关系
  10. 打怪升级的monteCarlo仿真方法