Skip to content

Commit 11399e1

Browse files
committed
Updated drag example
1 parent fd9d97e commit 11399e1

File tree

4 files changed

+32
-34
lines changed

4 files changed

+32
-34
lines changed

public/assets/pics/goblin.png

86.3 KB
Loading

public/assets/pics/slug.png

89.3 KB
Loading

public/assets/pics/spider.png

108 KB
Loading
Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,42 @@
1-
var config = {
2-
type: Phaser.WEBGL,
3-
parent: 'phaser-example',
4-
scene: {
5-
preload: preload,
6-
create: create
7-
}
8-
};
9-
10-
var game = new Phaser.Game(config);
11-
12-
function preload ()
1+
class Example extends Phaser.Scene
132
{
14-
this.load.image('eye', 'assets/pics/lance-overdose-loader-eye.png');
15-
}
16-
17-
function create ()
18-
{
19-
var image = this.add.sprite(200, 300, 'eye').setInteractive();
20-
21-
this.input.setDraggable(image);
22-
23-
// The pointer has to move 16 pixels before it's considered as a drag
24-
this.input.dragDistanceThreshold = 16;
3+
constructor()
4+
{
5+
super();
6+
}
257

26-
this.input.on('dragstart', function (pointer, gameObject) {
8+
preload ()
9+
{
10+
this.load.image('bg', 'assets/skies/cavern1.png');
11+
this.load.image('goblin', 'assets/pics/goblin.png');
12+
this.load.image('spider', 'assets/pics/spider.png');
13+
}
2714

28-
gameObject.setTint(0xff0000);
15+
create ()
16+
{
17+
this.add.image(400, 300, 'bg');
18+
this.add.text(16, 16, 'Move 32px before drag starts').setFontSize(24).setShadow(1, 1);
2919

30-
});
20+
this.add.image(200, 300, 'goblin').setInteractive({ draggable: true });
21+
this.add.image(600, 300, 'spider').setInteractive({ draggable: true });
3122

32-
this.input.on('drag', function (pointer, gameObject, dragX, dragY) {
23+
// The pointer has to move 32 pixels before it's considered as a drag
24+
this.input.dragDistanceThreshold = 32;
3325

34-
gameObject.x = dragX;
35-
gameObject.y = dragY;
26+
this.input.on('drag', (pointer, gameObject, dragX, dragY) => {
3627

37-
});
28+
gameObject.setPosition(dragX, dragY);
3829

39-
this.input.on('dragend', function (pointer, gameObject) {
30+
});
31+
}
32+
}
4033

41-
gameObject.clearTint();
34+
const config = {
35+
type: Phaser.AUTO,
36+
width: 800,
37+
height: 600,
38+
parent: 'phaser-example',
39+
scene: Example
40+
};
4241

43-
});
44-
}
42+
const game = new Phaser.Game(config);

0 commit comments

Comments
 (0)