forked from kaplayjs/kaplay
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fast loop using update (kaplayjs#513)
- Loading branch information
1 parent
913c20a
commit ff6defd
Showing
3 changed files
with
80 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// @ts-check | ||
|
||
kaplay(); | ||
|
||
loadBean(); | ||
|
||
const interval = 0.001; | ||
const delay = 1; | ||
|
||
loop(interval, () => { | ||
const bean = add([ | ||
sprite("bean"), | ||
pos(rand(vec2(0), vec2(width(), height()))), | ||
]); | ||
wait(delay, () => { | ||
destroy(bean); | ||
}); | ||
}, -1, true); | ||
|
||
const counter = add([ | ||
pos(10, 10), | ||
text(""), | ||
z(9999), | ||
]); | ||
add([ | ||
pos(counter.pos), | ||
rect(counter.width, counter.height), | ||
color(BLACK), | ||
z(counter.z - 1), | ||
{ | ||
update() { | ||
this.width = counter.width; | ||
this.height = counter.height; | ||
} | ||
} | ||
]) | ||
|
||
var beanCount = 0; | ||
onAdd(() => beanCount++); | ||
onDestroy(() => beanCount--); | ||
loop(0.1, () => { | ||
const error = 100 * Math.abs((delay / interval) - beanCount) / (delay / interval); | ||
counter.text = `${beanCount.toFixed().padStart(5)} beans\nerror: ${error.toFixed(2).padStart(5)}%`; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters