Когда вы добавляете прямоугольник в тело в Box2D, вы добавляете прибор. Мы можем идентифицировать прибор по его идентификатору.
this.myBody = new Phaser.Physics.Box2D.Body(this.game, null, 10, 10);
var fixture1 = this.myBody.addRectangle(2, 10, 10 * Math.cos(60), 10 * Math.sin(60), 60);
this.rect1_id = fixture1.id;
var fixture2 = this.myBody.addRectangle(2, 10, 10 * Math.cos(60), 10 * Math.sin(60), 60);
this.rect2_id = fixture2.id;
Затем мы можем добавить обратный вызов столкновения для спрайта. Это будет вызываться всякий раз, когда тело сталкивается с другим телом в этой категории. Обязательно установите категорию тел.
this.onCollision = function(thisBody, theirBody, thisFixture, theirFixture, begin, contact) {
// Parameters are:
// thisBody: the body of the sprite
// theirBody: the body of the sprite 'thisBody' collided with
// thisFixture, theirFixture: the fixtures of the bodies that collided
// begin: whether this was the start of the collision
// contact: the collision object
// I'll define what goes in this below
};
var category = 11;
this.body.setCollisionCategory(1);
this.body.setCollisionMask(1);
this.body.setCategoryContactCallback(
category,
this.onCollision,
this
);
Наконец, this.onCollision называется, мы можем просто проверить идентификатор зажимного приспособления и сравнить его со значениями, мы хранящимися в this.rect1_id и this.rect2_id.
this.onCollision = function(thisBody, theirBody, thisFixture, theirFixture, begin, contact) {
switch (thisFixture.id) {
case this.rect1_id:
console.log('hit the first rectangle!');
break;
case this.rect2_id:
console.log('hit the second rectangle!);
break;
default:
console.log("I don't recognise this fixture.");
}
};
Жаль этот ответ 2 лет! Я нашел его, ища что-то еще.