Simple game loop.
$ npm install --save @vaalentin/game-loop
import GameLoop from '@vaalentin/game-loop';
const loop = new GameLoop(30);
loop.on('init', () => {
// called once
});
loop.on('update', dt => {
// called 30 times per second
});
loop.on('render', dt => {
// called as fast as possible, usually 60 times a sec
});
GameLoop inherits from EventDispatcher.
Where fps
is a number that dictates how many times per second the update
will be called.
This is used to get a constant timestep (default is 60).
speed
is a number from (usually from 0
to 2
), to increase/reduce the timestep (default is 1
).
Start loop.
Stop loop.
Delete instance. Calls loop.stop()
.
init
Called once, when the loop starts for the first time.start
Called every time the loop starts.stop
Called every time the loop stops.update
Called at a constant interval, given by thefps
parameter.render
Called as fast asrequestAnimationFrame
can go.
MIT, see LICENSE.md for more details.