Arcade Physics Group vs Group Collision
Demonstrates how to handle collisions between two separate Arcade Physics groups, including using a collision callback to modify the individual objects involved.
800 × 600
class Example extends Phaser.Scene
{
preload ()
{
this.load.setBaseURL('https://cdn.phaserfiles.com/v410');
this.load.image('ball', 'assets/sprites/shinyball.png');
this.load.image('crate', 'assets/sprites/crate32.png');
}
create ()
{
// Create a few balls
const balls = this.physics.add.group({
key: 'ball',
quantity: 24,
bounceX: 1,
bounceY: 1,
collideWorldBounds: true,
velocityX: 300,
velocityY: 150
});
// Create a few crates
const crates = this.physics.add.group({
key: 'crate',
quantity: 24,
bounceX: 1,
bounceY: 1,
collideWorldBounds: true,
velocityX: -150,
velocityY: -300
});
Phaser.Actions.RandomRectangle(balls.getChildren(), this.physics.world.bounds);
Phaser.Actions.RandomRectangle(crates.getChildren(), this.physics.world.bounds);
this.physics.add.collider(
balls,
crates,
(ball, crate) =>
{
ball.setAlpha(0.5);
crate.setAlpha(0.5);
});
}
}
const config = {
scale: { mode: Phaser.Scale.FIT },
type: Phaser.AUTO,
width: 800,
height: 600,
parent: 'game-container',
physics: {
default: 'arcade',
arcade: {
debug: false
}
},
scene: Example
};
const game = new Phaser.Game(config);