COCOS2DX3.0 's parkour uses magnet props to absorb coins

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.