Hello, how can I achieve this situation? I have Matter sprite and bounds set to world dimensions, but when object collides nothing happend. I have own function to create enemy( createNewMachine() ), and when I create new one I add this collision, but it’s not working :(:
eventData show nothing. Colud you help me?
function createNewMachine(x: number, y: number) {
let machineID = Phaser.Math.Between(1, this.numberOfMachines);
let newMachine = this.matter.add
.sprite(x, y, `mach${machineID}-sprites`, `mach${machineID}_1.png`, {
shape: this[`mach${machineID}Shapes`][`mach${machineID}_1`]
})
.play(`play-mach${machineID}`)
.setIgnoreGravity(true);
this.matterCollision.addOnCollideStart({
objectA: this.matter.world.walls, //world bounds
objectB: newMachine, // some object to detect collision with
callback: eventData => {
// run logic here
console.log(eventData);
},
context: this // Context to apply to the callback function
});
this.machinesGroup.push(newMachine);
(window as any).mach = this.machinesGroup;
}
Can it be because these are the physical bodies but not the game objects? And wht code are you debugging? Fiddle? your code?
Also the pairs console logs are looking empty. I have run into that before. I have an idea but not sure. I have experimented with it in console but I saw unreliable results. I was even writing it as the cause but I have decided to delete it. But I can say that much even the pairs look empty the moment you check them the it was not once, it left a trail like Array(1), which means it had the pair inside it once.
I figured it out with matterCollision plugin. I just added all of my eniemes to Phaser.group ( array ) and call function below in update() , but call it in update() is not good idea, because for example function this.this.pigCollisionFunction() which decrements player’s life instead do that once it do several times, and the result is very laggy game and this(screen below):
const { left } = this.matter.world.walls; //left side of game bounds
this.matterCollision.addOnCollideStart({
objectA: this.machinesGroup.children.entries, // Array of enemies
objectB: [left],
callback: event => {
event.gameObjectA.setAlpha(0);
event.gameObjectA.destroy();
}
});