基本动作和组合动作实现了针对精灵的各种运动和动画效果的改变。但这种改变速度匀速的、线性的。通过ActionEase及其的派生类和Speed 类我们能够使精灵以非匀速或非线性速度运动,这样看起了效果更加逼真。

ActionEase的类图例如以下图所看到的。

以下我们通过一个实例介绍一下这些动作中速度的控制的使用,这个实比例如以下图所看到的,上图是一个操作菜单场景,选择菜单能够进入到下图动作场景,在下图动作场景中点击Gobutton能够运行我们选择的动作效果,点击Backbutton能够返回到菜单场景。

以下我们再看看详细的程序代码,首先看一下看HelloWorldScene.h文件,它的代码例如以下:

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__ #include "cocos2d.h"
#include "MyActionScene.h" typedef enum ①
{
kEaseIn = 1
,kEaseOut
,kEaseInOut
,kEaseSineIn
,kEaseSineOut
,kEaseSineInOut
,kEaseExponentialIn
,kEaseExponentialOut
,kEaseExponentialInOut
,kSpeed } ActionTypes; ② class HelloWorld : public cocos2d::Layer
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
void OnClickMenu(cocos2d::Ref* pSender); CREATE_FUNC(HelloWorld);
}; #endif // __HELLOWORLD_SCENE_H__

上述代码第①~②是定义个枚举类型ActionTypes,枚举类型ActionTypes中定义了10个常量。这10个常量相应10个菜单项。

HelloWorldScene的实现代码HelloWorldScene.ccp文件,它的主要代码例如以下:

bool HelloWorld::init()
{
if( !Layer::init() )
{
returnfalse;
} SizevisibleSize = Director::getInstance()->getVisibleSize();
Pointorigin = Director::getInstance()->getVisibleOrigin(); autobg = Sprite::create("background.png");
bg->setPosition(Point(visibleSize.width/2,visibleSize.height /2));
this->addChild(bg); autopItmLabel1 = Label::createWithBMFont("fonts/fnt2.fnt","EaseIn");
autopItmMenu1 = MenuItemLabel::create(pItmLabel1,
CC_CALLBACK_1(HelloWorld::OnClickMenu, this));
pItmMenu1->setTag(kEaseIn); autopItmLabel2 = Label::createWithBMFont("fonts/fnt2.fnt","EaseOut");
autopItmMenu2 = MenuItemLabel::create(pItmLabel2,
CC_CALLBACK_1(HelloWorld::OnClickMenu,this));
pItmMenu2->setTag(kEaseOut); autopItmLabel3 = Label::createWithBMFont("fonts/fnt2.fnt","EaseInOut");
autopItmMenu3 = MenuItemLabel::create(pItmLabel3,
CC_CALLBACK_1(HelloWorld::OnClickMenu,this));
pItmMenu3->setTag(kEaseInOut); autopItmLabel4 = Label::createWithBMFont("fonts/fnt2.fnt","EaseSineIn");
autopItmMenu4 = MenuItemLabel::create(pItmLabel4,
CC_CALLBACK_1(HelloWorld::OnClickMenu,this));
pItmMenu4->setTag(kEaseSineIn); autopItmLabel5 = Label::createWithBMFont("fonts/fnt2.fnt", "EaseSineOut");
autopItmMenu5 = MenuItemLabel::create(pItmLabel5,
CC_CALLBACK_1(HelloWorld::OnClickMenu,this));
pItmMenu5->setTag(kEaseSineOut); autopItmLabel6 = Label::createWithBMFont("fonts/fnt2.fnt","EaseSineInOut");
autopItmMenu6 = MenuItemSprite::create(pItmLabel6,
CC_CALLBACK_1(HelloWorld::OnClickMenu,this));
pItmMenu6->setTag(kEaseSineInOut); autopItmLabel7 = Label::createWithBMFont("fonts/fnt2.fnt","EaseExponentialIn");
autopItmMenu7 = MenuItemSprite::create(pItmLabel7,
CC_CALLBACK_1(HelloWorld::OnClickMenu,this));
pItmMenu7->setTag(kEaseExponentialIn); autopItmLabel8 = Label::createWithBMFont("fonts/fnt2.fnt","EaseExponentialOut");
autopItmMenu8 = MenuItemLabel::create(pItmLabel8,
CC_CALLBACK_1(HelloWorld::OnClickMenu,this));
pItmMenu8->setTag(kEaseExponentialOut); autopItmLabel9 = Label::createWithBMFont("fonts/fnt2.fnt","EaseExponentialInOut");
autopItmMenu9 = MenuItemLabel::create(pItmLabel9,
CC_CALLBACK_1(HelloWorld::OnClickMenu,this));
pItmMenu9->setTag(kEaseExponentialInOut); autopItmLabel10 = Label::createWithBMFont("fonts/fnt2.fnt","Speed");
autopItmMenu10 = MenuItemLabel::create(pItmLabel10,
CC_CALLBACK_1(HelloWorld::OnClickMenu,this));
pItmMenu10->setTag(kSpeed); automn = Menu::create(pItmMenu1,pItmMenu2,pItmMenu3,pItmMenu4,pItmMenu5,
pItmMenu6,pItmMenu7,pItmMenu8,pItmMenu9,pItmMenu10,NULL); mn->alignItemsInColumns(2,2, 2, 2, 2, NULL);
this->addChild(mn); returntrue;
} void HelloWorld::OnClickMenu(Ref* pSender)
{
MenuItem*nmitem = (MenuItem*)pSender; auto sc = Scene::create();
auto layer = MyAction::create();
layer->setTag(nmitem->getTag()); sc->addChild(layer); autoreScene = TransitionSlideInR::create(1.0f, sc);
Director::getInstance()->replaceScene(reScene);
}

在上诉代码大家比較熟悉了,我们这里就不再介绍了。以下我们再看看下一个场景MyActionScene,它的MyActionScene.ccp。它的主要代码例如以下:

void MyAction::goMenu(Ref* pSender)
{
log("Tag = %i",this->getTag());
FiniteTimeAction * ac1 = (FiniteTimeAction *)MoveBy::create(2,Point(200, 0));
FiniteTimeAction * ac2 = ((FiniteTimeAction *)ac1)->reverse(); ActionInterval * ac = Sequence::create(ac1, ac2, NULL); switch (this->getTag()) {
case kEaseIn:
sprite->runAction(EaseIn::create(ac, 3)); ①
break;
case kEaseOut:
sprite->runAction(EaseOut::create(ac, 3)); ②
break;
case kEaseInOut:
sprite->runAction(EaseInOut::create(ac,3)); ③
break;
case kEaseSineIn:
sprite->runAction(EaseSineIn::create(ac)); ④
break;
case kEaseSineOut:
sprite->runAction(EaseSineOut::create(ac)); ⑤
break;
case kEaseSineInOut:
sprite->runAction(EaseSineInOut::create(ac)); ⑥
break;
case kEaseExponentialIn:
sprite->runAction(EaseExponentialIn::create(ac)); ⑦
break;
case kEaseExponentialOut:
sprite->runAction(EaseExponentialOut::create(ac)); ⑧
break;
case kEaseExponentialInOut:
sprite->runAction(EaseExponentialInOut::create(ac)); ⑨
break;
case kSpeed:
sprite->runAction(Speed::create(ac, (CCRANDOM_0_1() * 5))); ⑩
break;
}
}

第①行代码sprite->runAction(EaseIn::create(ac, 3))是以3倍速度由慢至快。第②代码sprite->runAction(EaseOut::create(ac, 3))是以3倍速度由快至慢。

第③代码sprite->runAction(EaseInOut::create(ac, 3))是以3倍速度由慢至快再由快至慢。

第④代码sprite->runAction(EaseSineIn::create(ac))是採用正弦变换速度由慢至快。第⑤代码sprite->runAction(EaseSineOut::create(ac))是採用正弦变换速度由快至慢。

第⑥代码sprite->runAction(EaseOut::create(ac, 3)) 是採用正弦变换速度由慢至快再由快至慢。

第⑦代码sprite->runAction(EaseExponentialIn::create(ac))採用指数变换速度由慢至快。

第⑧代码sprite->runAction(EaseExponentialOut::create(ac))採用指数变换速度由快至慢。第⑨代码sprite->runAction(EaseExponentialInOut::create(ac)) 採用指数变换速度由慢至快再由快至慢。

第⑩代码sprite->runAction(Speed::create(ac, (CCRANDOM_0_1() * 5))) 随机设置变换速度。

《Cocos2d-x实战 C++卷》现已上线,各大商店均已开售:‍

京东:http://item.jd.com/11584534.html

亚马逊:http://www.amazon.cn/Cocos2d-x%E5%AE%9E%E6%88%98-C-%E5%8D%B7-%E5%85%B3%E4%B8%9C%E5%8D%87/dp/B00PTYWTLU

当当:http://product.dangdang.com/23606265.html

互动出版网:http://product.china-pub.com/3770734

《Cocos2d-x实战 C++卷》源代码及样章下载地址:

源代码下载地址:http://51work6.com/forum.php?mod=viewthread&tid=1155&extra=page%3D1

样章下载地址:

mod=viewthread&tid=1157&extra=page%3D1">http://51work6.com/forum.php?mod=viewthread&tid=1157&extra=page%3D1

欢迎关注智捷iOS课堂微信公共平台

 

Cocos2d-x怎样控制动作速度的更多相关文章

  1. Cocos2d-x如何控制动作速度

    基本动作和组合动作实现了针对精灵的各种运动和动画效果的改变.但这样的改变速度匀速的.线性的.通过ActionEase及其的派生类和Speed 类我们可以使精灵以非匀速或非线性速度运动,这样看起了效果更 ...

  2. 小尝试一下 cocos2d

    好奇 cocos2d 到底是怎样一个框架,正好有个项目需要一个游戏框架,所以稍微了解了一下.小结一下了解到的情况. 基本概念 首先呢,因为 cocos2d 是基于 pyglet 做的,你完全可以直接用 ...

  3. 在cocos2d里面如何使用Texture Packer和像素格式来优化spritesheet

    免责申明(必读!):本博客提供的所有教程的翻译原稿均来自于互联网,仅供学习交流之用,切勿进行商业传播.同时,转载时不要移除本申明.如产生任何纠纷,均与本博客所有人.发表该翻译稿之人无任何关系.谢谢合作 ...

  4. Cocos2d入门--2--三角函数的应用

    其实,三角函数的知识点是初中的数学基础.但是在编程里合理的利用的话,也会很好玩的,可以制作出很多有趣的动画特效. 首先要注意的是 角度 和 弧度 的转换. 360度 = 2×PI弧度 180度 =   ...

  5. ios游戏开发--cocos2d学习(1)

    学习cocos2d需要一定的编程基础,最好了解objective-c的语法.至于下载和安装的过程网上有很多,这里不多介绍,直接进入项目的学习. 创建一个cocos2d项目,直接运行,效果如图: 左下角 ...

  6. cocos2d粒子效果

    第9章 粒子效果 游戏开发者通常使用粒子系统来制作视觉特效.粒子系统能够发射大量细小的粒子并对他们进行渲染,而且效率要远高于渲染同样数目的精灵.粒子系统可以模拟下雨.火焰.雪.爆炸.蒸气拖尾以及其他多 ...

  7. 【Cocos2d入门教程二】Cocos2d-x基础篇

    上一章已经学习了环境的搭建.这一章对基础概念进行掌握.内容大概有: 1.导演 2.场景 3.节点 4.层 4.精灵 1.导演(Director) 导演存在的主要作用: a.环境设定(帧率 初始化ope ...

  8. Android导入Cocos2D的Sample项目

    导入Cocos2D项目到Android的Eclipse时注意以下几点 1. Set up Variables: Eclipse->Windows->Preferences->Gene ...

  9. 【转】频点CTO张成:基于Cocos2d的MMORPG开发经验

    http://www.9ria.com/plus/view.php?aid=27698 作者: zhiyuanzhe3 发表时间: 2013-06-29 17:46 6月29日,由9Tech社区.51 ...

随机推荐

  1. CSS中关于vertical-align垂直对齐

    一向以来,我对vertical-align的属性都搞的不是太清楚,今天刚好碰到有朋友问我相关的问题,于是自己潜心研究了一番,发现这玩意还真不是个简单的东西,在此我分享的东西都是抛弃脑残的IE的,如果你 ...

  2. jQuery新浪微博表情插件教程

    1.引入css文件 <link rel="stylesheet" type="text/css" href="jquery.sinaEmotio ...

  3. 《剑指offer》跳台阶

    一.题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 二.输入描述 输入n级台阶 三.输出描述 输出总有多少种不同跳法 四.牛客网提供的框架 cla ...

  4. jumpserver 安装python 报错

    环境centos7.5 pip3 insatll ./python-gssapi-0.6.4.tar.gz  报错 Command "python setup.py egg_info&quo ...

  5. HDU 4939 Stupid Tower Defense 简单DP

    题意: 地图为长为n个单位长度的直线,每通过一个单位长度需要t秒. 有3种塔,红塔可以在当前格子每秒造成x点伤害,绿塔可以在之后格子造成y点伤害,蓝塔可以使通过单位长度的时间增加z秒. 让你安排塔的排 ...

  6. [Recompose] Pass a React Prop to a Stream in RxJS

    When you declare your Component and Props in JSX, you can pass those props along to your RxJS stream ...

  7. quick 3.3载入Spine问题

    近期项目要升级到Quick 3.3, 而且使用了Spine作为动画编辑器, 在此把升级过程中.有关quick 3.3 载入Spine遇到的坑在此记录一下. 1.Spine版本号问题 首先Quick 3 ...

  8. 一个美丽的java烟花程序

    <img src="http://img.blog.csdn.net/20150625104525974?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi ...

  9. 同一台服务器部署多个WEB应用,SESSION冲突的解决方法

    由于一台服务器上使用Tomcat部署多个WEB项目,而项目因为用到框架都是一样的,导致同时运行,session相互冲突,这个登录后,那个就得重新登录,造成了使用不方便,解决办法如下: 在server. ...

  10. logAnalyzer日志管理系统配置实例

    LogAnalyzer日志管理系统配置实例 上个月我写过一篇<利用EventlogAnalyzer分析Linux日志>一文深受大家喜欢,今天我再次为大家讲解Linux系统下的一款开源的日志 ...