-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathloader.js
77 lines (63 loc) · 1.99 KB
/
loader.js
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
// @ts-check
// Customizing the asset loader
kaplay({
// Optionally turn off loading screen entirely
// Unloaded assets simply won't be drawn
// loadingScreen: false,
});
let spr = null;
// Every loadXXX() function returns a Asset<Data> where you can customize the error handling (by default it'll stop the game and log on screen), or deal with the raw asset data yourself instead of using a name.
loadSprite("bean", "/sprites/bean.png").onError(() => {
alert("oh no we failed to load bean");
}).onLoad((data) => {
// The promise resolves to the raw sprite data
spr = data;
});
loadSprite("ghosty", "/sprites/ghosty.png");
// load() adds a Promise under KAPLAY's management, which affects loadProgress()
// Here we intentionally stall the loading by 1sec to see the loading screen
load(
new Promise((res) => {
// wait() won't work here because timers are not run during loading so we use setTimeout
setTimeout(() => {
res();
}, 1000);
}),
);
// make loader wait for a fetch() call
load(fetch("https://kaboomjs.com/"));
// You can also use the handle returned by loadXXX() as the resource handle
const bugSound = loadSound("bug", "/examples/sounds/bug.mp3");
volume(0.1);
onKeyPress("space", () => play(bugSound));
// Custom loading screen
// Runs the callback every frame during loading
onLoading((progress) => {
// Black background
drawRect({
width: width(),
height: height(),
color: rgb(0, 0, 0),
});
// A pie representing current load progress
drawCircle({
pos: center(),
radius: 32,
end: map(progress, 0, 1, 0, 360),
});
drawText({
text: "loading" + ".".repeat(wave(1, 4, time() * 12)),
font: "monospace",
size: 24,
anchor: "center",
pos: center().add(0, 70),
});
});
onDraw(() => {
if (spr) {
drawSprite({
// You can pass raw sprite data here instead of the name
sprite: spr,
});
}
});