Simple Sprite Dragging
Demonstrates how to make a sprite interactive, enable it for dragging, and use the drag event to update its position as the pointer moves.
360 × 640
class Example extends Phaser.Scene
{
preload ()
{
this.load.setBaseURL('https://cdn.phaserfiles.com/v410');
this.load.image('apple', 'assets/sprites/apple.png');
}
create ()
{
const apple = this.add.sprite(100, 100, 'apple');
apple.setInteractive();
this.input.setDraggable(apple);
this.input.on('drag', (pointer, gameObject, dragX, dragY) =>
{
gameObject.x = dragX;
gameObject.y = dragY;
});
}
}
const config = {
scale: { mode: Phaser.Scale.FIT },
type: Phaser.AUTO,
width: 360,
height: 640,
_parent: 'game-container',
backgroundColor: '#efefef',
scene: Example
};
const game = new Phaser.Game(config);