第八章动作和动画-动作速度的控制

Cocos2d-x学习笔记


动作速度的控制

基本动作和组合动作实现了针对精灵的各种运动和动画效果的改变。带这样的改变速度是匀速的、线性的。通过ActionEase及其子类和Speed类,我们可以使精灵以非匀速或非线性速度运动。
ActionEase类图:

实例

HelloWorld.h文件

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
#include "SettingScene.h"

//枚举类型作为标签
typedef enum
{
    kEaseIn = 100,
    kEaseOut,
    kEaseInOut,
    kEaseSineIn,
    kEaseSineOut,
    kEaseSineInOut,
    kEaseExponentialIn,
    kEaseExponentialOut,
    kEaseExponentialInOut,
    kSpeed
}ActionTypes;

class HelloWorld : public cocos2d::Layer
{
public:
    static cocos2d::Scene* createScene();

    virtual bool init();

    // a selector callback
    void onClickMenu(cocos2d::Ref * pSender);

    // implement the "static create()" method manually
    CREATE_FUNC(HelloWorld);
};

#endif // __HELLOWORLD_SCENE_H__

HelloWorld.cpp文件

#include "HelloWorldScene.h"

USING_NS_CC;

Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();

    // 'layer' is an autorelease object
    auto layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }

    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

    /
    //背景
    auto bg = Sprite::create("bg.png");
    bg->setPosition(Vec2(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2));
    this->addChild(bg);

    auto pItemLabel1 = Label::createWithBMFont("fonts/fnt2.fnt", "EaseIn");
    auto pItemMenu1 = MenuItemLabel::create(pItemLabel1, CC_CALLBACK_1(HelloWorld::onClickMenu, this));
    pItemMenu1->setTag(kEaseIn);

    auto pItemLabel2 = Label::createWithBMFont("fonts/fnt2.fnt", "EaseOut");
    auto pItemMenu2 = MenuItemLabel::create(pItemLabel2, CC_CALLBACK_1(HelloWorld::onClickMenu, this));
    pItemMenu2->setTag(kEaseOut);

    auto pItemLabel3 = Label::createWithBMFont("fonts/fnt2.fnt", "EaseInOut");
    auto pItemMenu3 = MenuItemLabel::create(pItemLabel3, CC_CALLBACK_1(HelloWorld::onClickMenu, this));
    pItemMenu3->setTag(kEaseInOut);

    auto pItemLabel4 = Label::createWithBMFont("fonts/fnt2.fnt", "EaseSineIn");
    auto pItemMenu4 = MenuItemLabel::create(pItemLabel4, CC_CALLBACK_1(HelloWorld::onClickMenu, this));
    pItemMenu4->setTag(kEaseSineIn);

    auto  pItemLabel5 = Label::createWithBMFont("fonts/fnt2.fnt", "EaseSineOut");
    auto pItemMenu5 = MenuItemLabel::create(pItemLabel5, CC_CALLBACK_1(HelloWorld::onClickMenu, this));
    pItemMenu5->setTag(kEaseSineOut);

    auto  pItemLabel6 = Label::createWithBMFont("fonts/fnt2.fnt", "EaseSineInOut");
    auto pItemMenu6 = MenuItemLabel::create(pItemLabel6, CC_CALLBACK_1(HelloWorld::onClickMenu, this));
    pItemMenu6->setTag(kEaseSineInOut);

    auto  pItemLabel7 = Label::createWithBMFont("fonts/fnt2.fnt", "EaseExponentialIn");
    auto pItemMenu7 = MenuItemLabel::create(pItemLabel7, CC_CALLBACK_1(HelloWorld::onClickMenu, this));
    pItemMenu7->setTag(kEaseExponentialIn);

    auto  pItemLabel8 = Label::createWithBMFont("fonts/fnt2.fnt", "EaseExponentialOut");
    auto pItemMenu8 = MenuItemLabel::create(pItemLabel8, CC_CALLBACK_1(HelloWorld::onClickMenu, this));
    pItemMenu8->setTag(kEaseExponentialOut);

    auto  pItemLabel9 = Label::createWithBMFont("fonts/fnt2.fnt", "EaseExponentialInOut");
    auto pItemMenu9 = MenuItemLabel::create(pItemLabel9, CC_CALLBACK_1(HelloWorld::onClickMenu, this));
    pItemMenu9->setTag(kEaseExponentialInOut);

    auto  pItemLabel10 = Label::createWithBMFont("fonts/fnt2.fnt", "Speed");
    auto pItemMenu10 = MenuItemLabel::create(pItemLabel10, CC_CALLBACK_1(HelloWorld::onClickMenu, this));
    pItemMenu10->setTag(kSpeed);

    auto mn = Menu::create(pItemMenu1, pItemMenu2, pItemMenu3, pItemMenu4, pItemMenu5, pItemMenu6, pItemMenu7, pItemMenu8, pItemMenu9, pItemMenu10, NULL);
    mn->alignItemsInColumns(2, 2, 2, 2, 2, NULL);
    this->addChild(mn);

    return true;
}

void HelloWorld::onClickMenu(Ref * pSender)
{
    MenuItem * nmitem = (MenuItem *)pSender;

    //不使用Setting::createScene()创建,因为这样不能传递参数
    auto sc = Scene::create();
    auto layer = Setting::create();
    layer->setTag(nmitem->getTag());

    sc->addChild(layer);

    auto reScene = TransitionSlideInR::create(1.0f, sc);
    Director::getInstance()->replaceScene(reScene);
}

Setting.h文件

#ifndef __SETTING_SCENE_H__
#define __SETTING_SCENE_H__

#include "cocos2d.h"
#include "HelloWorldScene.h"

class Setting : public cocos2d::Layer
{
    cocos2d::Sprite * sprite;
public:
    static cocos2d::Scene* createScene();

    virtual bool init();

    // a selector callback
    void goMenu(cocos2d::Ref * pSender);
    void backMenu(cocos2d::Ref * pSender);

    // implement the "static create()" method manually
    CREATE_FUNC(Setting);
};

#endif // __SETTINGSCENE_SCENE_H__

Setting.cpp文件

#include "SettingScene.h"

USING_NS_CC;

Scene* Setting::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();

    // 'layer' is an autorelease object
    auto layer = Setting::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

bool Setting::init()
{
    //
    // 1. super init first
    if (!Layer::init())
    {
    return false;
    }

    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

    /
    //背景
    auto bg = Sprite::create("bg.png");
    bg->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
    this->addChild(bg);

    sprite = Sprite::create("hero.png");
    sprite->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
    this->addChild(sprite);

    auto backMenuItem = MenuItemImage::create("Back-up.png", "Back-down.png", CC_CALLBACK_1(Setting::backMenu, this));
    backMenuItem->setPosition(Director::getInstance()->convertToGL(Vec2(120, 100)));

    auto goMenuItem = MenuItemImage::create("Go-up.png", "Go-down.png", CC_CALLBACK_1(Setting::goMenu, this));
    goMenuItem->setPosition(Director::getInstance()->convertToGL(Vec2(visibleSize.width / 2, 100)));

    Menu * mn = Menu::create(backMenuItem, goMenuItem, NULL);
    mn->setPosition(Vec2::ZERO);
    this->addChild(mn);

    return true;
}

void Setting::backMenu(cocos2d::Ref * pSender)
{
    auto sc = HelloWorld::createScene();
    auto reScene = TransitionSlideInL::create(1.0f, sc);
    Director::getInstance()->replaceScene(reScene);
}

void Setting::goMenu(cocos2d::Ref * pSender)
{
    FiniteTimeAction *ac1 = (FiniteTimeAction *)MoveBy::create(2, Vec2(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));//3倍速度由慢至快
    break;
    case kEaseOut:
    sprite->runAction(EaseOut::create(ac, 3));//3倍速度有快至慢
    break;
    case kEaseInOut:
    sprite->runAction(EaseInOut::create(ac, 3));//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;
    default:
    break;
    }
}

函数调用

在顺序动作执行的中间或者结束时,可以回调某个函数,从而可以在该函数中执行任何处理。函数调用可以分为:无参数函数调用和有参数函数调用。

  • CC_CALLBACK_0:该宏定义了一个回调函数,并与函数对象绑定在一起,0表示这个函数没有输出参数。
  • CC_CALLBACK_1:该宏定义了一个回调函数,并与函数对象绑定在一起,1表示这个函数有一个输出参数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值