Cocos2dx3.2 compile common UI components (3) Collector effect Collector
Preface:
In the game, we often see that after scores are obtained, the scores are automatically raised to the corresponding score bar. Today we will achieve this effect.
Body:
This effect is actually very simple. You only need to add an action to the generated score (usually a Sprite or Node) so that it can be moved to the target position and then disappear. Here I provide a Collector class for you to use. Convenient operations.
Collector usage instructions: ① use the Collector: create () function to create a Collector object
② First, set the Collector location.
③ If you need to set the callback function after the element reaches the Collector, use Collector: setCallback (std: function ) Function
④ For the created Node, you only need to use Collector: addSource (node * Node) to add it to the collector object. Then the newly created node will automatically flow to the collector location.
⑤ Use the Collector: setTime function to set the floating time
Example:
c = Collector::create();c->setPosition(240,160);this->addChild(c);
void HelloWorld::menuCloseCallback(Ref* pSender){Sprite* sp = Sprite::create(CloseNormal.png);sp->setPosition(100,100);this->addChild(sp);c->addSource(sp);}
The effect is as follows:
Collector class is very simple. Let's look at the most important addSoruce function.
void Collector::addSource(Node* node){MoveTo* moveTo = MoveTo::create(m_time,this->getPosition());EaseBackInOut* easeMove = EaseBackInOut::create(moveTo);ScaleTo* scaleBig = ScaleTo::create(0.3*m_time,1.5f);ScaleTo* scaleSmall = ScaleTo::create(0.7*m_time,0.5f);Sequence* scale = Sequence::create(scaleBig,scaleSmall,NULL);Spawn* move = Spawn::create(easeMove,scale,NULL);CallFunc* callFunc = CallFunc::create([=](){node->removeFromParentAndCleanup(true);m_callback();});Sequence* action = Sequence::create(move,callFunc,NULL);node->runAction(action);}
The addSource function encapsulates a series of actions and then allows Node to execute them.