/
events-example.html
86 lines (79 loc) · 2.56 KB
/
events-example.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="//cdn.jsdelivr.net/npm/phaser@3.60.0/dist/phaser.js"></script>
<script src="../dist/iife/spine-phaser.js"></script>
<link rel="stylesheet" href="../../index.css" />
<title>Spine Phaser Example</title>
</head>
<body class="p-4 flex flex-col items-center">
<h1>Events example</h1>
<div style="display: flex; flex-direction: column; width: 800px">
<canvas id="game" width="800" height="600"></canvas>
<textarea id="log" style="height: 10em"></textarea>
</div>
</body>
<script>
function log(message) {
let log = document.querySelector("#log");
log.textContent += message + "\n";
log.scrollTop = log.scrollHeight;
console.log(message);
}
class EventsExample extends Phaser.Scene {
preload() {
this.load.spineBinary("spineboy-data", "assets/spineboy-pro.skel");
this.load.spineAtlas("spineboy-atlas", "assets/spineboy-pma.atlas");
}
create() {
const spineboy = this.add.spine(
400,
500,
"spineboy-data",
"spineboy-atlas"
);
spineboy.scale = 0.5;
spineboy.animationState.addListener({
start: (entry) => log(`Started animation ${entry.animation.name}`),
interrupt: (entry) =>
log(`Interrupted animation ${entry.animation.name}`),
end: (entry) => log(`Ended animation ${entry.animation.name}`),
dispose: (entry) => log(`Disposed animation ${entry.animation.name}`),
complete: (entry) =>
log(`Completed animation ${entry.animation.name}`),
});
spineboy.animationState.setAnimation(0, "walk", true);
const trackEntry = spineboy.animationState.addAnimation(
0,
"run",
3,
true
);
trackEntry.listener = {
event: (entry, event) =>
log(`Custom event for ${entry.animation.name}: ${event.data.name}`),
};
}
}
const config = {
canvas: document.querySelector("#game"),
width: 800,
height: 600,
type: Phaser.WEBGL,
scene: [EventsExample],
plugins: {
scene: [
{
key: "spine.SpinePlugin",
plugin: spine.SpinePlugin,
mapping: "spine",
},
],
},
};
const game = new Phaser.Game(config);
</script>
</html>