Pausing a Flash game or movie detecting its focus

Sometimes we want our Flash game or movie to stop when the user is not playing/watching it.

It’s a feature blog readers ask me frequently, because once they read the post, often they need to reload the page to enjoy the demostrative Flash movie as it was already running before readers could see it.

Also, this is what happens to our Flash movies when they are switched from visible to invisible, according to Tinic Uro, Adobe engineer:

When SWF is visible:

  • SWF frame rates are limited and aligned to jiffies, i.e. 60 frames a second. (Note that Flash Playe 10.1 Beta 3 still has an upper limit of 120 which will be changed before the final release)
  • timers (AS2 Interval and AS3 Timers) are limited and aligned to jiffies.
  • local connections are limited and aligned to jiffies. That means a full round trip from one SWF to another will take at least 33 milliseconds. Some reports we get say it can be up to 40ms.
  • video is NOT aligned to jiffies and can play at any frame rate. This increases video playback fidelity.

When SWF is invisible:

  • SWF frame rate is clocked down to 2 frames/sec. No rendering occurs unless the SWF becomes visible again.
  • timers (AS2 Interval and AS3 Timers) are clocked down to 2 a second.
  • local connections are clocked down to 2 a second.
  • video is decoded (not rendered or displayed) using idle CPU time only.
  • For backwards compatibility reasons we override the 2 frames/sec frame rate to 8 frames/sec when audio is playing.

For all these reasons, it’s necessary to pause the Flash game or movie when the SWF itself is not focused.

AS3 provides us flash.events.Event.ACTIVATE which is dispatched when Flash Player or an AIR application gains operating system focus and becomes active,and flash.events.Event.DEACTIVATE dispatched when Flash Player or an AIR application loses operating system focus and is becoming inactive.

Using these two listeners, we can quickly create a simple movie with a circle moving from left to right and a pause screen to be added once the game needs to be paused.

It’s a very basic script, but it works as we expect:

Click on the movie to gain focus, and outside the movie to lose the focus, and see what happens.

Download the source code.

  • Kevin

    Great Tutorial as always.
    Thanks!

  • Awesome! I’ll be using this in all my games to provide a better user experience!

    I got a little confused when you refer to ‘jiffys’, never really heard them referred that way before :D

    Very useful article, thanks!

  • I’m currently working on a Flash application that has a sequence of musical notes playing timed by EnterFrame events.

    When I switch focus, or make the .swf invisible, the sequence just keeps playing at the same rate, so “SWF frame rate is clocked down to 2 frames/sec. No rendering occurs unless the SWF becomes visible again.”
    is definitely NOT true.

    I always simply detect the MOUSE_LEAVE Event to switch to a framerate of 0.5 with this code:


    stage.addEventListener(Event.MOUSE_LEAVE, onStageLeave, false, 0, true);
    }

    public function onStageLeave(e:Event):void
    {
    stage.removeEventListener(Event.MOUSE_LEAVE, onStageLeave);
    stage.frameRate = 0.5;
    stage.addEventListener(MouseEvent.MOUSE_MOVE, onStageEnter, false, 0, true);
    }

    public function onStageEnter(e:MouseEvent):void
    {
    stage.removeEventListener(MouseEvent.MOUSE_MOVE, onStageEnter);
    stage.frameRate = (os == "Mac") ? 21 : 31;
    stage.addEventListener(Event.MOUSE_LEAVE, onStageLeave, false, 0, true);
    }

  • You can see this for yourself if you go to this blogpost of mine:

    http://blog.0l4f.com/?p=115

    This application does not have the MOUSE_LEAVE code. Just click on some notes, and switch to another window, making the .swf invisible. The music just keeps playing, eventhough it’s timing is frame-based.

  • gaffkins

    Thank you very much, I have a question. how to detect flash sleep mode, when i scroll page and i lose focus, frame rate drops to 2 fps.

  • erwin

    hi, thanks only that when i clicked the up down bar the movie still running!

  • Hm, okay, I just tested your example above, and when activated, the circle definitely stops moving when I scroll the browser down so that the movie is no longer visible.

    When I scroll it into view again, the circle starts moving again from the same spot.

    So, the ACTIVE / DEACTIVATE Events seem to fire as expected, but when you look at my example, there doesn’t seem to be any default behaviour attached to them as mentioned by Tinic Uro.

  • Pingback: PAUSING A FLASH GAME OR MOVIE DETECTING ITS FOCUS[???????????????????] « Flash Developer()

  • Pingback: PAUSING A FLASH GAME OR MOVIE DETECTING ITS FOCUS[???????????????????] « Flash Developer()

  • Pingback: PAUSING A FLASH GAME OR MOVIE DETECTING ITS FOCUS[???????????????????] « Flash Developer()

  • gaffkins

    When we look at the original plants vs. zombies, and scroll page losing focus, animations, sounds works correctly. How did they do?

  • Pingback: Daily Digest March 30th()

  • Ooh great thanks it is very usefull.

  • One thing to take into account when using this kind of technique is to use some kind of global tick event. This should be listened to (or be called) by all your animations etc. That is instead of using ENTER_FRAME events.

    I’ve found it very useful to have a custom written timer class that is also tied to the global tick, otherwise you’ll end up getting into problems when you’ve set a timer and the game / program pauses.

    Also, don’t forget to mute / unmute your sounds globally when leaving / pausing your app.

  • as2 has something to track if is visible like as3?

  • blue_damsel

    can I make a similar one like this on AS2?

  • M.A.D.

    How can I achieve this in AS2.0?

  • Brilliand

    This doesn’t pause if the user minimizes the browser or switches tabs – but both of those events cause the framerate reduction.

    It’s probably better to reduce your dependence on the framerate (or simply allow the framerate to dictate the speed of your game in a consistent way) than to add a specific hack such as this – though this technique is still useful for a quite different reason, that is, to save the user some frustration when he accidentally unfocuses and then can no longer control his character. The above cases aren’t a problem for that use, since if the user just minimized the window, he already knows what’s wrong.

  • tom

    Hi Emanuele,
    I would like to ask u a question – can u detect the situation, when u switch to another tab so Flash player lowers your FPS to save some performance? I have a multiplayer game, where both players are executing something, but when one of the players minimizes game, the second one have to wait for him. I would like to catch the situation, when the Flash player limits the FPS and do more during one frame. Can u help me catch the situation?

  • Pingback: Luis Espinal Blog | Pausing a Flash game or movie detecting its focus | Emanuele Feronato()