两个精灵碰撞,都是矩形,不想碰到边就碰撞
想再碰得深一点才算碰撞,就是缩小矩形碰撞的范围,这样如何实现?
我之前用的是cc.rectIntersectsRect()的方法
求大神帮助:964:
自定义了一个rectCreate 函数:
参数p:矩形的中心点
参数area:中心点距离上面距离area,距离左边距离area
cc.rectCreate = function (p, area) {
return cc.rect(p.x - area, p.y - area, area * 2, area * 2);
};
```
如何使用:
//碰撞一 普通
if (cc.rectIntersectsRect(child.getBoundingBox(), this.monster.getBoundingBox())) {
this.createParticle("around", monsterX, monsterY);
cc.AudioEngine.getInstance().playEffect("res/sounds/bomb.mp3", false);
child.removeFromParent(true);
}
//碰撞二 缩小矩形范围 更精准;
if (cc.rectIntersectsRect(cc.rectCreate(child.getPosition(), ), cc.rectCreate(this.monster.getPosition(), ))) {
this.createParticle("around", monsterX, monsterY);
cc.AudioEngine.getInstance().playEffect("res/sounds/bomb.mp3", false);
child.removeFromParent(true);
}
//碰撞三 扩大碰撞区域 200dp就发生了碰撞
if (cc.rectIntersectsRect(cc.rectCreate(child.getPosition(), ), cc.rectCreate(this.monster.getPosition(), ))) {
this.createParticle("around", monsterX, monsterY);
cc.AudioEngine.getInstance().playEffect("res/sounds/bomb.mp3", false);
child.removeFromParent(true);
}
//碰撞四 两个精灵中心距离小于40时
var distance = cc.pDistance(child.getPosition(), this.monster.getPosition());
if (distance < 40) {
this.createParticle("around", monsterX, monsterY);
cc.AudioEngine.getInstance().playEffect("res/sounds/bomb.mp3", false);
child.removeFromParent(true);
}
```
关于矩形碰撞的详细博文:
http://blog.csdn.net/touchsnow/article/details/29290617
:846:万分感谢,我尝试下!