Basic Sprite Dragging
Demonstrates how to make a sprite draggable by setting the interactive draggable property and updating its position during the drag event.
800 × 600
class Example extends Phaser.Scene
{
constructor()
{
super();
}
preload ()
{
this.load.setBaseURL('https://cdn.phaserfiles.com/v410');
this.load.image('bg', 'assets/skies/gradient29.png');
this.load.image('char', 'assets/pics/nayuki.png');
}
create ()
{
this.add.image(400, 300, 'bg');
this.add.text(16, 16, 'Drag the Sprite').setFontSize(24).setShadow(1, 1);
const sprite = this.add.sprite(400, 300, 'char');
sprite.setInteractive({ draggable: true });
sprite.on('drag', (pointer, dragX, dragY) => sprite.setPosition(dragX, dragY));
}
}
const config = {
scale: { mode: Phaser.Scale.FIT },
type: Phaser.AUTO,
width: 800,
height: 600,
parent: 'game-container',
scene: Example
};
const game = new Phaser.Game(config);