标签:cocos2d-js cocos2d-html5 碰撞检测
游戏中的碰撞还是比较多的,比如角色与角色的碰撞,角色与墙壁的碰撞,角色与怪物的碰撞等,都需要var dollRect = sprite.getBoundingBox();
var dollHeadRect = this.catchHand.getBoundingBox();
if(cc.rectIntersectsRect(dollRect, dollHeadRect)){
//发生碰撞事件
}
2、第二种是在网上找到的,我感觉有些麻烦,不过相对于第一种,对于不规则物体支持的好了一些
这里根据BoundingBox 的 上下左右的中间点来判断碰撞,使检测的更准确一些 var box1 = sprite1.getBoundingBox();
var bottom = cc.p(box1.x +box1.width / 2,box1.y);
var right = cc.p(box1.x +box1.width,box1.y +box1.height / 2);
var left = cc.p(box1.x,box1.y +box1.height / 2);
var top = cc.p(box1.x + box1.width / 2,box1.y + box1.height);
var box2 = sprite2.getBoundingBox();
if(cc.rectContainsPoint(box2, left)||cc.rectContainsPoint(box2, right)||cc.rectContainsPoint(box2, top)||cc.rectContainsPoint(box2, bottom)){
//发生碰撞
} var sprite = this.dolls3[i];
var distance = cc.pDistance(this.catchHand.getPosition(), sprite.getPosition());
var radiusSum = sprite.radius + this.catchHand.radius;
cc.log("distance:" + distance + "; radius:" + radiusSum);
if(distance < radiusSum){
//发生碰撞
}
//针对第三三种方法又加深了一下,使得对矩形类的精灵也能有好的判断,
//主要就是分别对X和Y方向设置不同的Radius,然后去进行分别判断
var distanceX = Math.abs(this.catchHand.getPositionX() - sprite.getPositionX());
var distanceY = Math.abs(this.catchHand.getPositionY() - sprite.getPositionY());
var radiusYSum = sprite.radiusY + this.catchHand.radius;
if(distanceX < sprite.radiusX && distanceY < radiusYSum){
this.catchDollSucceed(sprite);
return;
}更多cocos2d-html5开发文章可以关注牛人 touchsnow的博客:http://blog.makeapp.co
也可以去我的个人博客站点:Melove 我爱http://www.melove.net
cocos2d-html5 碰撞检测的几种方法,布布扣,bubuko.com
标签:cocos2d-js cocos2d-html5 碰撞检测
原文地址:http://blog.csdn.net/lzan13/article/details/30247447