cocos2d-x中實現簡單碰撞檢測的有rect包圍盒工具,用起來十分便捷。先看**:
//子彈與敵機的碰撞
for (int i=0; ipx,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)) else
if (nowenemy->toolid>0)
allenemy.eraseobject(nowenemy);
nowenemy->removefromparentandcleanup(true);
j--;
}break;}}
}
其實只是宣告了兩個rect包圍盒,其中前兩個值是兩個點,後面兩個是從那兩個點開始取多長的座標。
然後我們在用intersectsrect檢測兩個包圍盒有沒有碰撞就可以了。是不是很簡單?
cocos2dx碰撞檢測實現
在此場景中,我們假設有很多敵人和子彈,其中子彈可以擊中敵人,並且碰撞後子彈和敵人都會隨之消失。首先,我們需要去追蹤這些敵人和子彈。我們可以為這兩種物體新增tag,在這裡我們給敵人新增tag 1,給子彈新增tag 2,因為ccsprite繼承自ccnode,因為已經有了settag和gettag方法 ...
cocos碰撞檢測
兩個矩形的碰撞檢測,主要看boundingbox是否接觸 rect rc1 spriteauto getboundingbox rect rc2 spritectrl getboundingbox if rc1.intersectsrect rc2 則認為碰撞 兩個圓的碰撞檢測,看圓心距離 poin...
Cocos2d x3 2 多點觸控
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 include cocos2d.h using ns cc classgamescene publiccocos2d layer 簡介 cocos2d x 3.0...