Cocos2d-x如何控制动作速度

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

ActionEase的类图如下图所示。

下面我们通过一个实例介绍一下这些动作中速度的控制的使用这个实例如下图所示上图是一个操作菜单场景选择菜单可以进入到下图动作场景在下图动作场景中点击Go按钮可以执行我们选择的动作效果点击Back按钮可以返回到菜单场景。

下面我们再看看具体的程序代码首先看一下看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 

样章下载地址http://51work6.com/forum.php?mod=viewthread&tid=1157&extra=page%3D1

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

 

 

时间: 2024-07-28 14:04:53

Cocos2d-x如何控制动作速度的相关文章

Cocos2D游戏项目CCTableView在Xcode7.2下的无法滚动问题

大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 一个RPG游戏转换到Xcode7.2下发现一个问题,原来可以上下滚动的CCTableView控件现在不可以滚动了. 控制台中可以看到输出的警告: WARNING: A Gesture recognizer (<CCTapDownGestureRecognizer: 0x611000042e40; baseClass = UIGestureRecognizer;

jquery 菜单移动-这是一个菜单移动的操作,但是顺序a向上移动有错误,求指导,请问怎么控制不超出父节点

问题描述 这是一个菜单移动的操作,但是顺序a向上移动有错误,求指导,请问怎么控制不超出父节点 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 无标题文档 #wrap{margin:20px auto;width: 500px;} #wrap li{list-st

cocos2dx3.3开发FlappyBird总结十一:控制层功能设计

控制层的任务就是监听触摸事件,然后回调代理方法.控制层并不具体处理任务事情,只是抛给代理处理,因此需要先设计一个代理. 代理只是一个方法,那就是触摸: /** * The delegate between option layer and game layer */ class OptionDelegate { public: /** * When touch the option layer, it will be called */ virtual void onTouch() = 0; }

《Cocos2D权威指南》——1.4 深入学习HelloCocos2D项目

1.4 深入学习HelloCocos2D项目 在完成了第一个HelloCocos2D项目后,如果读者不仅想看到飞机在屏幕上飞行,还想知道这一切是怎样实现的,我们不妨来一起探究其中的每一行代码.1.4.1 初识场景和节点 要想理解HelloCocos2D这个项目,首先要了解场景(CCScene).层(CCLayer)和节点(CCNode)的概念. Cocos2D游戏是由不同的场景构成的,由导演(CCDirector)负责运行和切换各个场景.在Cocos2D中,CCDirector在任何一个时间点上

《Cocos2D权威指南》——3.8 垂直射击游戏—加载游戏数据

3.8 垂直射击游戏-加载游戏数据 为了使大家对CCSprite和各相关类的使用有更加直观的印象,下面我们结合前面的游戏示例,使用精灵表单优化游戏性能,同时在游戏开始和结束时添加菜单,让玩家对游戏有更多控制权.当然,在这个示例小游戏中,这种优化是看不出差别的.但这是最佳实践,建议读者以后编写游戏都以这种方式使用精灵. **3.8.1 注释draw方法和背景 ** 首先,在Xcode中打开之前的项目中把draw方法注释掉,同时恢复先前注释掉的添加游戏背景的代码段,编译并运行,如图3-6所示. 注意

cocos2d logoPython的2D游戏开发框架 cocos2d

问题描述 cocos2d 是一个 Python 用来开发 2D 游戏和其他图形化交互应用的框架.**主要特性****界面流程控制:** Manage the flow control between different scenes in an easy way**精灵:** Fast and easy sprites**动作:** Just tell sprites what you want them to do. Composable actions like _move_, _rotat

8 cocos2dx添加场景切换效果,控制场景切换彻底完成之后再执行动画

 1 添加场景切换效果 供场景切换的类: CCTransitionJumpZoom CCTransitionProgressRadialCCW CCTransitionProgressRadialCW CCTransitionProgressHorizontal CCTransitionProgressVertical CCTransitionProgressInOut CCTransitionProgressOutIn CCTransitionCrossFade CCTransitionF

5.cocos2dx中关于draw绘图,声音和音效,预加载,播放与停止Vs暂停和恢复,音量控制

 1 draw绘图 头文件 T19Draw.h #ifndef __T19Draw_H__ #define __T19Draw_H__ #include "cocos2d.h" #include "TBack.h"   USING_NS_CC;   class T19Draw:public TBack { public:     static CCScene * scene();     CREATE_FUNC(T19Draw);     bool init()

《Cocos2D权威指南》——第2章 你的第一款iPhone游戏—垂直射击游戏 2.1 准备工作

第2章 你的第一款iPhone游戏-垂直射击游戏 本章我们将以一个垂直射击游戏为题材,带领大家动手制作一个简单的游戏,主要目的是让大家对Cocos2D开发游戏有一个感性的认识,同时体验Cocos2D的强大以及易用性.之后的章节将引入更多的游戏元素,逐步完善此游戏. 2.1 准备工作 作为开发者,首先需要有一台iOS设备,iPhone.iPod Touch或者iPad都可以:其次要拥有开发者账号,按照第1章介绍的方法下载并安装开发者证书,这样才可以把游戏编译运行到真机上.为什么非要真机呢?接下来向