Cocos2d-x中实现简单碰撞检测的有Rect包围盒工具,用起来十分便捷。先看代码:
//子弹与敌机的碰撞
for (int i=0; i<allBullet.size(); i++) {
Bullet * nowBullet=allBullet.at(i);
for (int j=0; j<allEnemy.size(); j++) {
Enemy * nowEnemy = allEnemy.at(j);
Rect br(nowBullet->px,nowBullet->py-100,nowBullet->sp->getContentSize().width,nowBullet->sp->getContentSize().height);
Rect er(nowEnemy->ex,nowEnemy->ey,nowEnemy->eSprite->getContentSize().width,nowEnemy->eSprite->getContentSize().height);
if (br.intersectsRect(er)) {
allBullet.eraseObject(nowBullet);
nowBullet->removeFromParentAndCleanup(true);
i--;
nowEnemy->hp=nowEnemy->hp-1;
if (nowEnemy->hp<=0) {
if (nowEnemy->type&#