Cocos2dx collision detection implementation

Source: Internet
Author: User

In this scenario, we assume that there are many enemies and bullets in which the bullets can hit the enemy, and after the collision, the bullets and the enemy will disappear. First, we need to track these enemies and bullets. We can add tags for these two objects. Here we Add tag = 1 to the enemy, and Add tag = 2 to the bullet ,. Because CCSprite inherits from CCNode, because the setTag and getTag methods are already available, we can use this method to distinguish different elves. Add two member objects in HelloWorldScene. h. Used to store enemies and bullets respectively. [Cpp] protected: cocos2d: CCArray * _ targets; cocos2d: CCArray * _ projectiles; In cocos2dx, initialize the two variables in the constructor, in init () new in the function and release in the destructor. [Cpp] // initialize _ targets = new CCArray; _ projectiles = new CCArray; HelloWorld ::~ HelloWorld () {if (_ targets) {_ targets-> release (); _ targets = NULL;} if (_ projectiles) {_ projectiles-> release (); _ projectiles = NULL;} // cpp does not need to call super release. The virtual destructor will do these tasks} HelloWorld: HelloWorld (): _ targets (NULL ), _ projectiles (NULL) {} Now changes addTarget () to add a new enemy to the targets array and sets its tag to 1. [cpp] // Add to the array target-> setTag (1); _ targets-> addObject (target); change ccTouchesEnded () to add a new bullet to the bullets array and set its tag 2. [cpp] // Add to the _ projectiles array projectile-> setTag (2); _ projectiles-> addObject (projectile); next, change the spriteMoveFinished () function as follows. Here we will remove some genie from the corresponding array. [Cpp] void HelloWorld: spriteMoveFinished (CCNode * sender) {CCSprite * sprite = (CCSprite *) sender; this-> removeChild (sprite, true ); if (sprite-> getTag () = 1) // target {_ targets-> removeObject (sprite);} else if (sprite-> getTag () = 2) // projectile {_ projectiles-> removeObject (sprite) ;}} the following function update () will be executed once per frame, which is generally used to detect collision detection, you can remove collision bullets and enemies. Declare it in HelloWorldScene. h and define it in HelloWorldScene. cpp. [Cpp] void HelloWorld: update (float dt) {CCArray * projectilesToDelete = new CCArray; CCArray * targetsToDelete = new CCArray; CCObject * it = NULL; CCObject * jt = NULL; CCARRAY_FOREACH (_ bullet, it) {CCSprite * projectile = dynamic_cast <CCSprite *> (it); CCRect projectileRect = CCRectMake (projectile-> getPosition (). x-(projectile-> getContentSize (). width/2), projectile-> getPosition (). y-(projectile-> g EtContentSize (). height/2), projectile-> getContentSize (). width, projectile-> getContentSize (). height); CCARRAY_FOREACH (_ target, jt) {CCSprite * target = dynamic_cast <CCSprite *> (jt); CCRect targetRect = CCRectMake (target-> getPosition (). x-(target-> getContentSize (). width/2), target-> getPosition (). y-(target-> getContentSize (). height/2), target-> getContentSize (). width, target-> getContentSize (). height ); If (projectileRect. intersectsRect (targetRect) {targetsToDelete-> addObject (target); projectilesToDelete-> addObject (projectile) ;}} CCARRAY_FOREACH (targetsToDelete, jt) {CCSprite * target = dynamic_cast <CCSprite *> (jt); _ target-> removeObject (target); this-> removeChild (target, true);} CCARRAY_FOREACH (projectilesToDelete, it) {CCSprite * projectile = dynamic_cast <CCSprite *> (it); _ bullet-> remov EObject (projectile); this-> removeChild (projectile, true);} projectilesToDelete-> release (); targetsToDelete-> release (); OK, the last thing we will do is to add update () to the timer so that it can be called once every frame. Generally, this line of code is added to the onEnter () function. [Cpp] void HelloWorld: onEnter () {CCLayer: onEnter (); this-> schedule (schedule_selector (HelloWorld: update ));}

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.