Jump to content

Identify objects in collisionstart


adeptnix
 Share

Recommended Posts

Hi guys,

I have different objects in my game, something like this

scene.matter.add.image(x, y, 'car');
scene.matter.add.image(x, y, 'cone1');
scene.matter.add.image(x, y, 'man');

And I try identify what kind of objects collides

this.matter.world.on('collisionstart', (event, bodyA, bodyB) => {
    if (bodyA.label === 'car' && bodyB.label === 'cone') {
        bodyB.cone.body.setTexture('cone2');
    }
    if (bodyA.label === 'cone' && bodyB.label === 'car') {
        bodyA.cone.body.setTexture('cone2');
    }

    if (bodyA.label === 'man' && bodyB.label === 'cone') {
        bodyB.cone.body.setTexture('cone1');
    }
    if (bodyA.label === 'cone' && bodyB.label === 'man') {
        bodyA.cone.body.setTexture('cone1');
    }
});

But sometimes cone object don't change his texture despite I hit it by a car.

If it can help, full code there - https://gitlab.com/grigoriytretyakov/phaser3-racing-car/blob/master/src/Game.js#L172

 

Link to comment
Share on other sites

Turns out that I should iterate over pairs:

 

this.matter.world.on('collisionstart', (event) => {
    event.pairs.forEach(pair => {                                  
        const { bodyA, bodyB  } = pair;

        if (bodyA.label === 'car' && bodyB.label === 'cone') {
            bodyB.cone.down();
        }
        else if (bodyA.label === 'cone' && bodyB.label === 'car') {
            bodyA.cone.down();
        }
        else if (bodyA.label === 'man' && bodyB.label === 'cone') {
            bodyB.cone.up();
        }
        else if (bodyA.label === 'cone' && bodyB.label === 'man') {
            bodyA.cone.up();
        }
    });

    if (this.cones.filter(cone => cone.isUp).length === 0) {
        this.scene.start('GameOver');
    }
});

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...