第八章动作和动画-间隔动作

Cocos2d-x学习笔记


间隔动作

间隔动作执行完成需要一定的事件,可以设置duration属性来设置动作的执行时间。间隔动作的基类是ActionInterval。

实例

HelloWorld.h文件

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

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

//枚举类型作为标签
typedef enum
{
    kMoveTo = 100,
    kMoveBy,
    kJumpTo,
    kJumpBy,
    kBezierBy,
    kScaleTo,
    kScaleBy,
    kRotateTo,
    kRotateBy,
    kBlink,
    kTintTo,
    kTintBy,
    kFadeTo,
    kFadeIn,
    kFadeOut
}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);

    //XxxTo是运动到指定位置,XxxBy是运动到相对位置
    auto pItemLabel1 = Label::createWithBMFont("fonts/fnt2.fnt", "MoveTo");
    auto pItemMenu1 = MenuItemLabel::create(pItemLabel1, CC_CALLBACK_1(HelloWorld::onClickMenu, this));
    pItemMenu1->setTag(kMoveTo);

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

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

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

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

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

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

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

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

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

    auto  pItemLabel11 = Label::createWithBMFont("fonts/fnt2.fnt", "TintTo");
    auto pItemMenu11 = MenuItemLabel::create(pItemLabel11, CC_CALLBACK_1(HelloWorld::onClickMenu, this));
    pItemMenu11->setTag(kTintTo);

    auto  pItemLabel12 = Label::createWithBMFont("fonts/fnt2.fnt", "TintBy");
    auto pItemMenu12 = MenuItemLabel::create(pItemLabel12, CC_CALLBACK_1(HelloWorld::onClickMenu, this));
    pItemMenu12->setTag(kTintBy);

    auto  pItemLabel13 = Label::createWithBMFont("fonts/fnt2.fnt", "FadeTo");
    auto pItemMenu13 = MenuItemLabel::create(pItemLabel13, CC_CALLBACK_1(HelloWorld::onClickMenu, this));
    pItemMenu13->setTag(kFadeTo);

    auto  pItemLabel14 = Label::createWithBMFont("fonts/fnt2.fnt", "FadeIn");
    auto pItemMenu14 = MenuItemLabel::create(pItemLabel14, CC_CALLBACK_1(HelloWorld::onClickMenu, this));
    pItemMenu14->setTag(kFadeIn);

    auto  pItemLabel15 = Label::createWithBMFont("fonts/fnt2.fnt", "FadeOut");
    auto pItemMenu15 = MenuItemLabel::create(pItemLabel15, CC_CALLBACK_1(HelloWorld::onClickMenu, this));
    pItemMenu15->setTag(kFadeOut);

    auto mn = Menu::create(pItemMenu1, pItemMenu2, pItemMenu3, pItemMenu4, pItemMenu5, pItemMenu6, pItemMenu7, pItemMenu8, pItemMenu9, pItemMenu10, pItemMenu11, pItemMenu12, pItemMenu13, pItemMenu14, pItemMenu15, NULL);
    mn->alignItemsInColumns(3, 3, 3, 3, 3, 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("Background800x480.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)
{
    Size size = Director::getInstance()->getVisibleSize();
    Vec2 p = Vec2(CCRANDOM_0_1() * size.width, CCRANDOM_0_1() * size.height);

    ccBezierConfig bezier;//贝塞尔曲线结构体

    switch (this->getTag())
    {
    //MoveTo,MoveBy是移动函数
    case kMoveTo:
    sprite->runAction(MoveTo::create(2, Vec2(size.width - 50, size.height - 50)));
    break;
    case kMoveBy:
    sprite->runAction(MoveBy::create(2, Vec2(-50, -50)));
    break;
    //JumpTo,JumpBy是跳动函数,第一个参数是持续的时间(s),第二个参数是跳动到的位置,第三个参数是跳动的高度,第四个参数是跳动的次数
    case kJumpTo:
    sprite->runAction(JumpTo::create(2, Vec2(150, 50), 30, 5));
    break;
    case kJumpBy:
    sprite->runAction(JumpBy::create(2, Vec2(100, 100), 30, 5));
    break;
    //贝赛尔曲线
    case kBezierBy:
    bezier.controlPoint_1 = Vec2(0, size.height / 2);//第一个控制点
    bezier.controlPoint_2 = Vec2(300, -size.width / 2);//第二个控制点
    bezier.endPosition = Vec2(100, 100);//结束点
    break;
    //缩放动作
    case kScaleTo:
        sprite->runAction(ScaleTo::create(2, 0.5));//(时间,缩放比例)
    break;
    case kScaleBy:
    sprite->runAction(ScaleBy::create(2, 0.5));//(时间,缩放比例)
    break;
    //旋转动作函数
    case kRotateTo:
       sprite->runAction(RotateTo::create(2, 180));//(时间,旋转角度)
       break;
    case kRotateBy:
    sprite->runAction(RotateBy::create(2, -180));//(时间,旋转角度)
    break;
    //闪烁动作函数
    case kBlink:
    sprite->runAction(Blink::create(3, 5));//(时间,闪烁次数)
    break;
    //染色动作函数
    case kTintTo:
    sprite->runAction(TintTo::create(2, 255, 0, 0));//(时间, R, G, B)
    break;
    case kTintBy:
       sprite->runAction(TintBy::create(0.5, 0, 255, 255));//(时间, R, G, B)
    break;
    //不透明度变换函数
    case kFadeTo:
    sprite->runAction(FadeTo::create(1, 80));//(时间,不透明度)80%
    break;
    //淡入淡出函数0~255,0为完全透明,255为完全不透明
    case kFadeIn:
       sprite->runAction(FadeIn::create(1));//
       break;
    case kFadeOut:
       sprite->runAction(FadeOut::create(1));
       break;
    default:
    break;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值