首页 > 代码库 > Cocos2d-x学习笔记(十一)瞬时动作
Cocos2d-x学习笔记(十一)瞬时动作
动作类Action是一切动作的祖先类。它有三个直接继承子类:
- FiniteTimeAction受时间限制的动作;
- Follow精灵跟随精灵的动作;
- 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);}
在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);}
运行结果:
图1 HelloWorld层
图2 MyAction层
注意:这里的聚焦貌似没那么准确。
Cocos2d-x学习笔记(十一)瞬时动作
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。