I’m trying to make a loop to my game and I found this solution, but I’m struggling with making the function to cancel the loop. Here is my class where I’m getting the animation frame and I have function like “start”, which I’m calling when I want to start the game loop.
class Engine {
static renderFrame = getRequestAnimationFrame();
tick() {
console.log("test");
Engine.renderFrame(() => {
this.tick();
});
}
start() {
// Start the game ticking
Engine.renderFrame(() => {
this.tick();
});
}
}
Here is the “getRequestAnimationFrame” function, which I’m calling.
export const getRequestAnimationFrame = () => {
const fallback = callback => setTimeout(callback, 1000 / 30);
return (requestAnimationFrame || fallback).bind(window);
};
This is the function I’m trying to make, which should stop the game loop and also when I’ll call again “start” function it should be looping again, but nothing I tried worked (it probably isn’t even close).
end() {
(Engine.renderFrame).unbind(window);
}