-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathsprite.js
129 lines (112 loc) · 2.86 KB
/
sprite.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// @ts-check
// Sprite animation
// Start a kaboom game
kaplay({
// Scale the whole game up
scale: 4,
// Set the default font
font: "monospace",
});
// Loading a multi-frame sprite
loadSprite("dino", "/examples/sprites/dino.png", {
// The image contains 9 frames layed out horizontally, slice it into individual frames
sliceX: 9,
// Define animations
anims: {
"idle": {
// Starts from frame 0, ends at frame 3
from: 0,
to: 3,
// Frame per second
speed: 5,
loop: true,
},
"run": {
from: 4,
to: 7,
speed: 10,
loop: true,
},
// This animation only has 1 frame
"jump": 8,
},
});
const SPEED = 120;
const JUMP_FORCE = 240;
setGravity(640);
// Add our player character
const player = add([
sprite("dino"),
pos(center()),
anchor("center"),
area(),
body(),
]);
// .play is provided by sprite() component, it starts playing the specified animation (the animation information of "idle" is defined above in loadSprite)
player.play("idle");
// Add a platform
add([
rect(width(), 24),
area(),
outline(1),
pos(0, height() - 24),
body({ isStatic: true }),
]);
// Switch to "idle" or "run" animation when player hits ground
player.onGround(() => {
if (!isKeyDown("left") && !isKeyDown("right")) {
player.play("idle");
}
else {
player.play("run");
}
});
player.onAnimEnd((anim) => {
if (anim === "idle") {
// You can also register an event that runs when certain anim ends
}
});
onKeyPress("space", () => {
if (player.isGrounded()) {
player.jump(JUMP_FORCE);
player.play("jump");
}
});
onKeyDown("left", () => {
player.move(-SPEED, 0);
player.flipX = true;
// .play() will reset to the first frame of the anim, so we want to make sure it only runs when the current animation is not "run"
if (player.isGrounded() && player.curAnim() !== "run") {
player.play("run");
}
});
onKeyDown("right", () => {
player.move(SPEED, 0);
player.flipX = false;
if (player.isGrounded() && player.curAnim() !== "run") {
player.play("run");
}
});
["left", "right"].forEach((key) => {
onKeyRelease(key, () => {
// Only reset to "idle" if player is not holding any of these keys
if (player.isGrounded() && !isKeyDown("left") && !isKeyDown("right")) {
player.play("idle");
}
});
});
const getInfo = () =>
`
Anim: ${player.curAnim()}
Frame: ${player.frame}
`.trim();
// Add some text to show the current animation
const label = add([
text(getInfo(), { size: 12 }),
color(0, 0, 0),
pos(4),
]);
label.onUpdate(() => {
label.text = getInfo();
});
// Check out https://kaboomjs.com#SpriteComp for everything sprite() provides