Get Child World Position
Demonstrates how to calculate the global world coordinates of a child object inside a moving Container using the local transform matrix.
800 × 600
class Example extends Phaser.Scene
{
preload ()
{
this.load.setBaseURL('https://cdn.phaserfiles.com/v410');
this.load.image('lemming', 'assets/sprites/lemming.png');
}
create ()
{
const container = this.add.container(0, 300);
const sprite = this.add.sprite(0, 0, 'lemming');
const text = this.add.text(10, 10);
container.add(sprite);
this.tweens.add({
targets: container,
x: 800,
duration: 3000,
yoyo: true,
repeat: -1,
onUpdate: function ()
{
const p = container.localTransform.transformPoint(sprite.x, sprite.y);
text.setText([
sprite.x,
sprite.y,
p.x,
p.y
]);
}
});
}
}
const config = {
scale: { mode: Phaser.Scale.FIT },
type: Phaser.AUTO,
width: 800,
height: 600,
backgroundColor: '#010101',
parent: 'game-container',
scene: Example
};
const game = new Phaser.Game(config);