官方给的demo里,在碰撞事件里,为什么不直接检测两个物体碰撞,二是用到了上次碰撞的框?这是为什么呢?附上代码?
在demo中 HeroControl.js中部分代码
onCollisionEnter: function(other, self) {
this.node.color = cc.Color.RED;
this.touchingNumber++;
var otherAabb = other.world.aabb;
var selfAabb = self.world.aabb.clone();
// 上一次计算的碰撞组件的 aabb 碰撞框
var preAabb = self.world.preAabb;
selfAabb.x = preAabb.x;
selfAabb.y = preAabb.y;
selfAabb.x = self.world.aabb.x;**//这里为什么要分别给x和y赋值检测碰撞呢?**
if (cc.Intersection.rectRect(selfAabb, otherAabb)) {
if (this.speed.x < 0 && (selfAabb.xMax > otherAabb.xMax)) {
this.node.x = otherAabb.xMax;
this.collisionX = -1;
} else if (this.speed.x > 0 && (selfAabb.xMin < otherAabb.xMin)) {
this.node.x = otherAabb.xMin - selfAabb.width;
this.collisionX = 1;
}
this.speed.x = 0;
other.touchingX = true;
return;
}