title: How do I call this function correctly?
body: <p>I am working though Eloquent Javascript.  I'm at the end of chapter 15, "A Platform Game", and I'm having trouble figuring out how to get the game to pause.  </p>
  
  <p>One of the exercises at the end of the chapter asks you to do this:
  "Make it possible to pause (suspend) and unpause the game by pressing the Esc key.</p>
  
  <p>This can be done by changing the runLevel function to use another keyboard event 
  handler and interrupting or resuming the animation whenever the Esc key is hit."  The author says you can do this by rearranging the way runLevel calls runAnimation.
  Here is the function referred to:  </p>
  
  <pre><code>function runLevel(level, Display, andThen) {
      var display = new Display(document.body, level);
      runAnimation(function(step) {
        level.animate(step, arrows);
        display.drawFrame(step);
        if (level.isFinished()) {
          display.clear();
          if (andThen)
            andThen(level.status);
          return false;
        }
      });
    }
  </code></pre>
  
  <p>I modified the function thus:</p>
  
  <pre><code>  function runLevel(level, Display, andThen) {
      var display = new Display(document.body, level);
      var paused = false;
  
      addEventListener("keydown", function(event){
          if (event.keyCode == 27) {
              paused = !paused;
          }
      });
  
      if (!paused){
          runAnimation(function(step) {
            level.animate(step, arrows);
            display.drawFrame(step);
            if (level.isFinished()) {
              display.clear();
              if (andThen)
                andThen(level.status);
              return false;
            }
          });
      }
  
    }
  </code></pre>
  
  <p>But the game continues to play on, even after I hit escape.  I do not understand why.<br>
  The full code of the game can be found here:
  <a href="http://eloquentjavascript.net/code/chapter/15_game.js" rel="nofollow">http://eloquentjavascript.net/code/chapter/15_game.js</a>
  Thanks in advance for any help!</p>