Skip to content

Commit c3a36a3

Browse files
committed
fix: stop using async in lifespan, rename particle example to lifespan
1 parent 0076e3c commit c3a36a3

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed
Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// @ts-check
22

3-
// Particle spawning
43

54
kaplay();
65

@@ -18,26 +17,25 @@ sprites.forEach((spr) => {
1817

1918
setGravity(800);
2019

21-
// Spawn one particle every 0.1 second
22-
loop(0.1, () => {
23-
// TODO: they are resolving collision with each other for some reason
24-
// Compose particle properties with components
20+
// Spawn one object every 0.1 second
21+
loop(0.2, () => {
22+
// Compose object properties with components
2523
const item = add([
2624
pos(mousePos()),
2725
sprite(choose(sprites)),
2826
anchor("center"),
2927
scale(rand(0.5, 1)),
3028
area({ collisionIgnore: ["particle"] }),
3129
body(),
32-
lifespan(1, { fade: 0.5 }),
30+
// lifespan() comp destroys the object after desired seconds
31+
lifespan(1, {
32+
// it will fade after 0.5 seconds
33+
fade: 0.5
34+
}),
3335
opacity(1),
3436
move(choose([LEFT, RIGHT]), rand(60, 240)),
3537
"particle",
3638
]);
3739

38-
item.onCollide("particle", (p) => {
39-
console.log("dea");
40-
});
41-
4240
item.jump(rand(320, 640));
4341
});

src/components/misc/lifespan.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,23 @@ export function lifespan(time: number, opt: LifespanCompOpt = {}): EmptyComp {
2424
id: "lifespan",
2525
require: ["opacity"],
2626
async add(this: GameObj<OpacityComp>) {
27-
await _k.game.root.wait(time);
28-
this.opacity = this.opacity ?? 1;
29-
if (fade > 0) {
30-
await _k.game.root.tween(
31-
this.opacity,
32-
0,
33-
fade,
34-
(a) => this.opacity = a,
35-
easings.linear,
36-
);
37-
}
38-
this.destroy();
27+
_k.game.root.wait(time, () => {
28+
this.opacity = this.opacity ?? 1;
29+
if (fade > 0) {
30+
_k.game.root.tween(
31+
this.opacity,
32+
0,
33+
fade,
34+
(a) => this.opacity = a,
35+
easings.linear,
36+
).onEnd(() => {
37+
this.destroy();
38+
});
39+
}
40+
else {
41+
this.destroy();
42+
}
43+
});
3944
},
4045
};
4146
}

0 commit comments

Comments
 (0)