"COCOS2D-X-JS Series" Collision detection

Source: Internet
Author: User

A simple description of the game's collision detection principle: First to specify a collision detection area for each game object, and then in the Update method in real-time detection of two objects in the region whether there is overlap, if there is, then a collision occurs.

Simply look at the code, which is extracted from the official code.

1Tools. Collisionhelper={2Iscollided:function(CCA,CCB) {3         varAx = cca.x, ay = cca.y, bx = ccb.x, by =ccb.y;4         /*if (Math.Abs (AX-BX) > 5| | Math.Abs (ay-by) >5) {5 return false;6         }*/7         varArect = This. Makecolliderect (CcA);8         varBrect = This. Makecolliderect (CcB);9         returnCc.rectintersectsrect (Arect, brect);Ten     }, OneMakecolliderect:function(CcA) { A         returnCc.rect (cca.x-3, cca.y-3, 20, 20); -     } -};

Simply say the code: Collision detection is compared to each game object in the collision area (the above code we set the area aspect is 20, the area coordinates is the current game object coordinates minus 3, this 3 must be based on different objects in the game to dynamically adjust), to get the two areas where the location, Send the Cc.rectinterestsrect function to it. In fact within the Cc.rectinterestsrect function is also done in the area range detection.

If you want to hide the game object after a collision, use it directly: Gameobject.setvisibility (false).

Words with collision detection results with Cc.fadein and cc.fadeout can be very simple to make a collision animation out.

"COCOS2D-X-JS Series" Collision detection

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.