碰撞平台范例中关于onCollisionEnter的问题。
onCollisionEnter: function (other, self) {
this.node.color = cc.Color.RED;
this.touchingNumber ++;
var otherAabb = other.world.aabb.clone();//为什么要clone()而不能直接赋值,如果把.clone()拿掉,星星也不能落在平台上,为什么?
var selfAabb = self.world.aabb;
var preAabb = self.world.preAabb;
selfAabb.x = preAabb.x;
selfAabb.y = preAabb.y; //如果注释掉这句,那么星星就不能落在平台上 ,为什么?
selfAabb.x = self.world.aabb.x;//selfAabb本来就赋了self.world.aabbb, 后来又赋了preAabb,现在又是self.world.aabb,是不是多此一举?
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;
}
selfAabb.y = self.world.aabb.y; //selfAabb本来就赋了self.world.aabbb, 后来又赋了preAabb,现在又是self.world.aabb,是不是多此一举?
if (cc.Intersection.rectRect(selfAabb, otherAabb)) {
if (this.speed.y < 0 && (selfAabb.yMax > otherAabb.yMax)) {
this.node.y = otherAabb.yMax;
this.jumping = false;
this.collisionY = -1;
}
else if (this.speed.y > 0 && (selfAabb.yMin < otherAabb.yMin)) {
this.node.y = otherAabb.yMin - selfAabb.height;
this.collisionY = 1;
}
this.speed.y = 0;
other.touchingY = true;
}
},
谢谢,不过还是有问题。
既然selfAabb.x = self.world.aabb.x是把物体回退判断碰撞的方向,那么为什么还要之前还要这两句获得上次计算的结果?
selfAabb.x = preAabb.x;
selfAabb.y = preAabb.y;
但是如果不加的话,物体会掉落穿过平台。
还有这句
var otherAabb = other.world.aabb.clone();
为什么要clone()? 如果不加的话,也是物体会穿过平台。
