0

I'm having trouble with quiting/killing/exitting my requestAnimationFrame. My gameloop works, i can pause my game (gameloop stays active). But when I want to return to my menu, I want to kill the requestAnimationFrame, so it stops drawing and updating. I've searched on the internet and stackoverflow, found similar questions tried te response, but no luck :(

var fps = 60;
var now;
var then = Date.now();
var interval = 1000/fps;
var delta;  

//============================================================================

function gameloop(){
window.requestAnimationFrame(gameloop);

if (game.isUnpaused()){
//game is not paused, update all
      now = Date.now();
          delta = now - then;

              if (delta > interval) {
                  then = now - (delta % interval);
              //DO ALL WHAT'S NEEDED: draw avatar,move obstacles,move avatar....
                  }}

//what to do when game is paused:
else{//draw stuff when game is paused}

}

Can Anybody help me?

Thanks in advance

1

1 Answer 1

2
var active = true;
function gameloop(){
    if(active){
        window.requestAnimationFrame(gameloop);
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.