1. Data Structure Selection
// The efficiency of array insertion and deletion is low; the efficiency of searching and traversing is high.
// High efficiency of list insertion and deletion; low efficiency of searching and traversing
// Add or delete: the number of collisions between monsters and darts is small.
// Traversal: it takes more than 1.0 fps for a traversal.
// Select array
2. Collision Detection Functions
void HelloWorld::update(float delta) // delta = 1.0 / fps{CCArray* targetToDelete = new CCArray;CCArray* projToDelete = new CCArray;CCObject* itarget;CCObject* iproj;CCARRAY_FOREACH(_targets, itarget){CCSprite* target = (CCSprite*)itarget;CCRect targetZone = CCRectMake(target->getPositionX(),target->getPositionY(),target->getContentSize().width,target->getContentSize().height);CCARRAY_FOREACH(_projs, iproj){CCSprite* proj = (CCSprite*)iproj;CCRect projZone = CCRectMake(proj->getPositionX(),proj->getPositionY(),proj->getContentSize().width,proj->getContentSize().height);if (projZone.intersectsRect(targetZone)){projToDelete->addObject(iproj);targetToDelete->addObject(itarget);}} // end of iterate projectile} // end of iterate targetCCARRAY_FOREACH(projToDelete, iproj){_projs->removeObject(iproj);CCSprite* proj = (CCSprite*)iproj;proj->removeFromParentAndCleanup(true);}CCARRAY_FOREACH(targetToDelete, itarget){_targets->removeObject(itarget);CCSprite* target = (CCSprite*)itarget;target->removeFromParentAndCleanup(true);}targetToDelete->release();projToDelete->release();}
Note: You cannot delete a Member from CCArray while traversing CCArray.
3. Use CCSprite-> setTag (int)
In this way, different genie can be marked to distinguish them.
Uploaded to Youku
Http://v.youku.com/v_show/id_XNjY0MjEzNzI0.htmlLesson 1
Http://v.youku.com/v_show/id_XNjY0MjMzMzYw.html 2nd
Http://v.youku.com/v_show/id_XNjY0MjU1OTcy.html 3rd
Http://v.youku.com/v_show/id_XNjY0MzQxMDE2.html 4th
Http://v.youku.com/v_show/id_XNjY1MTI3NjYw.html 5th
Http://v.youku.com/v_show/id_XNjY1MTI5ODQw.html 6th
Please download HD from Baidu online storage.
Reprinted please indicate the source: http://blog.csdn.net/xingxiliang/article/details/18557631