第八章动作和动画-瞬时动作

Cocos2d-x学习笔记


基本动作

动作(Action)包括了基本动作和基本动作的组合;基本动作包括缩放、移动、旋转等动作,而且这些动作变化的速度也可以设定。

Action的一个子类是FiniteTimeAction,FiniteTimeAction是一种受时间限制的动作,Follow是一种允许精灵跟随另一个精灵的动作,Speed是在一个动作运行时改变其运动速率。
ActionInstantActionInterval是两种不同风格的动作类,ActionInstant封装了一种瞬时动作,ActionInterval封装了一种间隔动作。
在Node类中关于动作的函数如下:

  • Action * runAction(Action * action):运行指定动作,返回值仍然是一个动作对象。
  • void stopAction(Action * action):停止指定动作。
  • void stopActionByTag(int tag):通过制定标签来停止动作。
  • void stopAllActions():停止所有的动作。

瞬时动作

实例

HelloWorld.h文件

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

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

//枚举类型作为标签
typedef enum
{
    PLACE_TAG = 102,
    FLIPX_TAG,
    FLIPY_TAG,
    HIDE_SHOW_TAG,
    TOGGLE_TAG
}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("Background800x480.png");
    bg->setPosition(Vec2(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2));
    this->addChild(bg);

    //新的创建MenuItem方式,先创建标签后创建MenuItem
    auto placeLabel = Label::createWithBMFont("fonts/fnt2.fnt", "Place");
    auto placeMenu = MenuItemLabel::create(placeLabel, CC_CALLBACK_1(HelloWorld::onClickMenu, this));
    placeMenu->setTag(PLACE_TAG);

    auto flipXLabel = Label::createWithBMFont("fonts/fnt2.fnt", "FlipX");
    auto flipXMenu = MenuItemLabel::create(flipXLabel, CC_CALLBACK_1(HelloWorld::onClickMenu, this));
    flipXMenu->setTag(FLIPX_TAG);

    auto flipYLabel = Label::createWithBMFont("fonts/fnt2.fnt", "FlipY");
    auto flipYMenu = MenuItemLabel::create(flipYLabel, CC_CALLBACK_1(HelloWorld::onClickMenu, this));
    flipYMenu->setTag(FLIPY_TAG);

    auto hideLabel = Label::createWithBMFont("fonts/fnt2.fnt", "Hide or Show");
    auto hideMenu = MenuItemLabel::create(hideLabel, CC_CALLBACK_1(HelloWorld::onClickMenu, this));
    hideMenu->setTag(HIDE_SHOW_TAG);

    auto  toggleLabel = Label::createWithBMFont("fonts/fnt2.fnt", "Toggle");
    auto toggleMenu = MenuItemLabel::create(toggleLabel, CC_CALLBACK_1(HelloWorld::onClickMenu, this));
    toggleMenu->setTag(TOGGLE_TAG);

    auto mn = Menu::create(placeMenu, flipXMenu, flipYMenu, hideMenu, toggleMenu, NULL);
    mn->alignItemsVertically();
    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
{
    bool hiddenFlag;//用来保持精灵隐藏的状态
    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("Plane.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);

    this->hiddenFlag = true;

    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);

    switch (this->getTag())
    {
    case PLACE_TAG:
    sprite->runAction(Place::create(p));
    break;
    case FLIPX_TAG:
    sprite->runAction(FlipX::create(true));//水平方向翻转
    break;
    case FLIPY_TAG:
    sprite->runAction(FlipY::create(true));//垂直方向翻转
    break;
    case HIDE_SHOW_TAG:
    if (hiddenFlag)
        sprite->runAction(Hide::create());
    else
        sprite->runAction(Show::create());
    hiddenFlag = !hiddenFlag;
    break;
    case TOGGLE_TAG:
    sprite->runAction(ToggleVisibility::create());//将Node对象显示/隐藏
    break;
    default:
    break;
    }
}

你可能感兴趣的:(Cocos2d-x)