COCOS2DX3.0 's parkour uses magnet props to absorb coins
- COCOS2DX30 's parkour uses magnet props to absorb coins
- Principle
- Implementation methods and key codes
Reprint please specify [http://blog.csdn.net/JANESTAR/article/details/45268851]
In the parkour game, we often need to have such a scene, runner in the course of parkour can eat gold coins, when he ate a magnet props, the screen in the range of all the gold coins will be flying towards the runner, to achieve the effect of eating gold.
- Principle
- Implementation methods and key codes
- *
Principle
In fact, the principle is very simple, we design the process of running cool game, in fact, the x-coordinate of runner has not changed, is the background and props in the backward movement to produce parkour effect. When the runner jump, is actually to give runner an upward initial speed, let him carry on free fall. The cocos2dx3.0 integrates the physical engine chipmunk. We can easily get the dynamic coordinates of the runner.
After obtaining the coordinates of the runner, we can use a simple action to achieve the effect of gold adsorption. In particular, we can use the action for gold coins as long as the gold doesn't move out of the screen.
Implementation methods and key codes
Let me briefly explain the internship method and the key code
Auto Contactlistenner =eventlistenerphysicscontact:: Create (); Contactlistenner->oncontactbegin = [ This] (physicscontact &contact) {Auto b_1 = (sprite*)contact. Getshapea ()GetBody ()GetNode (); Auto b_2 = (sprite*)contact. Getshapeb ()GetBody ()GetNode ();//Magnets absorb gold coinsif(B_1->gettag () = = Magnettag | | B_2->gettag () = = Magnettag) {b_1->setvisible (false); _basemanager->getcoinsbymagnet (_runner); }return false; };Director:: getinstance ()Geteventdispatcher ()Addeventlistenerwithscenegraphpriority (Contactlistenner, This);}void Basemanager:: Getcoinsbymagnet (runner* Runner) { for(Auto coin: _coinvec) {if(Coin->getpositionx () >coin->getconsize (). width/2) {VEC2 pos = runner->getphysicsbody ()GetPosition (); Auto ACTIONTo =MoveTo:: Create (0.6, POS); Coin->runaction (ACTIONTo); } }}
The code should be very simple, I will not explain ....
Cocos2dx3.0 's parkour uses magnet props to absorb gold coins