Shuffle Group Children
Demonstrates how to use Phaser.Actions.Shuffle to randomly reorder the render depth of children within a Group.
800 × 600
class Example extends Phaser.Scene
{
preload ()
{
this.load.setBaseURL('https://cdn.phaserfiles.com/v410');
this.load.image('phaser', 'assets/sprites/phaser.png');
this.load.image('atari', 'assets/sprites/atari400.png');
this.load.image('bikku', 'assets/sprites/bikkuriman.png');
this.load.image('block', 'assets/sprites/block.png');
this.load.image('can', 'assets/sprites/cokecan.png');
}
create ()
{
const group = this.add.group();
group.create(230, 200, 'atari');
group.create(400, 200, 'phaser');
group.create(480, 200, 'bikku');
group.create(540, 200, 'block');
group.create(600, 200, 'can');
this.input.on('pointerdown', pointer =>
{
// Shuffle the children
const children = Phaser.Actions.Shuffle(group.getChildren());
// Assign their new depth based on their position within the Group
for (let i = 0; i < children.length; i++)
{
children[i].setDepth(i);
}
}, this);
}
}
const config = {
scale: { mode: Phaser.Scale.FIT },
type: Phaser.AUTO,
width: 800,
height: 600,
backgroundColor: '#2d2d2d',
parent: 'game-container',
scene: Example
};
const game = new Phaser.Game(config);