melonJS
    Preparing search index...

    Function setFocusEvents

    • Let the browser window report the game's focus, or leave it to the game's host (see focusEvents).

      Two separate settings decide what happens when the player switches away:

      • WHO reports it: the browser window by default, whose blur, focus and visibilitychange events the engine turns into event.BLUR and event.FOCUS. This function turns that on or off.
      • WHAT the game does then: the application's pauseOnBlur, stopOnBlur and resumeOnFocus flags. They apply to every event.BLUR and event.FOCUS, wherever it comes from.

      To keep a game running while the player is away, turn the flags off (app.pauseOnBlur = false): the window still reports, and nothing pauses.

      To let a host decide when the game runs, such as a web game portal's SDK (YouTube Playables forbids pausing on the page's visibility) or a native wrapper, turn this off instead and keep the flags: the window then reports nothing, the host's callbacks emit event.BLUR and event.FOCUS, and the flags react to them exactly as they did to the window, silencing the sound effects, releasing held keys and undoing only a pause the engine applied. Turning the flags off would not do it: they cannot tell the window's events from the host's, so the host's pause would do nothing either.

      It can be called before or after the application is created.

      Parameters

      • enable: boolean

        true (the default) to follow the browser window, false to leave the focus to the game's host

      Returns void

      // YouTube Playables: only YouTube's own events pause and resume the game
      me.device.setFocusEvents(false);
      app.stopOnBlur = true; // a pause stops the rendering too
      app.pauseOnBlur = false;
      ytgame.system.onPause(() => me.event.emit(me.event.BLUR));
      ytgame.system.onResume(() => me.event.emit(me.event.FOCUS));