Wrap Sprite
Demonstrates how to use the physics world wrap method to keep a moving sprite within the screen boundaries by wrapping it to the opposite side.
800 × 600
class Example extends Phaser.Scene
{
block;
preload ()
{
this.load.setBaseURL('https://cdn.phaserfiles.com/v410');
this.load.image('block', 'assets/sprites/block.png');
}
create ()
{
this.block = this.physics.add.image(0, 0, 'block').setVelocity(150, 150);
}
update ()
{
this.physics.world.wrap(this.block, 48);
}
}
const config = {
scale: { mode: Phaser.Scale.FIT },
type: Phaser.AUTO,
width: 800,
height: 600,
parent: 'game-container',
physics: {
default: 'arcade'
},
scene: Example
};
const game = new Phaser.Game(config);