Nested Get Bounds Test
Demonstrates how to accurately calculate the world-space bounds of a sprite nested inside multiple transformed containers using the getBounds method.
800 × 600
class Example extends Phaser.Scene
{
graphics;
bounds;
container;
image;
preload ()
{
this.load.setBaseURL('https://cdn.phaserfiles.com/v410');
this.load.image('disk', 'assets/sprites/copy-that-floppy.png');
}
create ()
{
this.image = this.add.image(0, 0, 'disk');
this.container = this.add.container(0, 0, [ this.image ]);
const container2 = this.add.container(400, 300, [ this.container ]);
this.graphics = this.add.graphics();
this.bounds = this.image.getBounds();
this.tweens.add({
targets: container2,
duration: 2000,
scaleX: 2,
scaleY: 2,
ease: 'Sine.easeInOut',
repeat: -1,
yoyo: true
});
}
update ()
{
this.container.rotation += 0.015;
this.bounds = this.image.getBounds();
this.graphics.clear();
this.graphics.lineStyle(1, 0xffff00);
this.graphics.strokeRectShape(this.bounds);
}
}
const config = {
scale: { mode: Phaser.Scale.FIT },
type: Phaser.AUTO,
parent: 'game-container',
width: 800,
height: 600,
backgroundColor: '#2d8d2d',
scene: Example
};
const game = new Phaser.Game(config);