首页 > 代码库 > Cocos2d-x学习笔记(十一)瞬时动作

Cocos2d-x学习笔记(十一)瞬时动作

动作类Action是一切动作的祖先类。它有三个直接继承子类:

  1. FiniteTimeAction受时间限制的动作;
  2. Follow精灵跟随精灵的动作;
  3. Speed运动速度控制;

而FiniteTimeAction又有两个直接子类:分别是ActionInstant和ActionInterval,顾类名而思意。

瞬时动作即立即执行动作,下边是其使用示例:

技术分享
void MyAction::goMenu(cocos2d::Ref *pSender){    log("Tag = %i", this->getTag());    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));// Places the node in a certain position            break;        case FLIPX_TAG:            sprite->runAction(FlipX::create(true));// Flips the sprite horizontally            break;        case FLIPY_TAG:            sprite->runAction(FlipY::create(true));// Flips the sprite vertically            break;        case HIDE_SHOW_TAG:            if (hiddenFlag)            {                sprite->runAction(Hide::create());// Hide the node                hiddenFlag = false;            }            else            {                sprite->runAction(Show::create());// Show the node                hiddenFlag = true;            }            break;        case TOGGLE_TAG:            sprite->runAction(ToggleVisibility::create());// Toggles the visibility of a node            break;        default:            break;    }}    void MyAction::backMenu(cocos2d::Ref *pSender){    auto sc = HelloWorld::createScene();    // Slide in the incoming scene from the left border.    auto reScene = TransitionSlideInL::create(1.0f, sc);    Director::getInstance()->replaceScene(reScene);}
View Code

在goMenu()函数中的动作类都是ActionInstant的子类;

技术分享
void HelloWorld::OnClickMenu(cocos2d::Ref *pSender){    MenuItem *nmitem = (MenuItem*)pSender;    auto sc = Scene::create();    auto layer = MyAction::create();    layer->setTag(nmitem->getTag());// set layer‘s tag, and this should be the most important part in this func.    sc->addChild(layer);    auto reScene = TransitionSlideInR::create(1.0f, sc);    Director::getInstance()->replaceScene(reScene);}
View Code

运行结果:

技术分享

图1 HelloWorld层

 

技术分享

图2 MyAction层

注意:这里的聚焦貌似没那么准确。

 

Cocos2d-x学习笔记(十一)瞬时动作