[cocos2d-x][游戏开发]通过cocos2d-x实现简易飞机大战 08.游戏界面 碰撞检测

来源:互联网 发布:游戏源码怎么用 编辑:程序博客网 时间:2024/05/07 13:42

从游戏里可以看出,当飞机和子弹碰到一起的时候就会爆炸,我们要做的就是实现怎么确定飞机和子弹碰到了一起;

我们将子弹与飞机的图层包围起来(包围核),当两个包围核的坐标重叠活着接触的时候我们就可以认为这两个无题已经碰在了一起,我们也可以添加爆炸效果或者移除飞机图层

 <p class="p1"><span class="s1">void</span> <span class="s2">Game</span>::update(<span class="s1">float</span> t)</p><p class="p1">{</p>   <span style="font-family: Arial, Helvetica, sans-serif;">if (allEnemy.size()==0||allButter.size()==0) {</span>
        return;    }    //       for (int j=0; j<allTool.size(); j++) {        //得到第j架敌机        Tool * nowtool=allTool.at(j);        Rect br(plane->px,                plane->py,                plane->getChildByTag(10)->getContentSize().width,                plane->getChildByTag(10)->getContentSize().height);        Rect er(nowtool->tx,                nowtool->ty,                nowtool->sp->getContentSize().width,                nowtool->sp->getContentSize().height);        if (br.intersectsRect(er))        {                          //敌机消失            allTool.eraseObject(nowtool);            nowtool->removeFromParentAndCleanup(true);            j--;        }    }    for (int j=0; j<allEnemy.size(); j++) {        //得到第j架敌机        Enemy * nowEnemy=allEnemy.at(j);        Rect br(plane->px,                plane->py,                plane->getChildByTag(10)->getContentSize().width,                plane->getChildByTag(10)->getContentSize().height);        Rect er(nowEnemy->ex,                nowEnemy->ey,                nowEnemy->eSprite->getContentSize().width,                nowEnemy->eSprite->getContentSize().height);        if (br.intersectsRect(er))        {  //碰撞检测到2个矩形相交            //敌机消失            allEnemy.eraseObject(nowEnemy);            nowEnemy->removeFromParentAndCleanup(true);            j--;                            }        } 

0 0
原创粉丝点击