Tag Archives: Deactivate

Flash Optimization – Activate & Deactivate Events

Flash optimization is becoming increasing important & with alternative development tools that developers can use. We all have a responsibility to build Flash elements that are efficient as much as they are groundbreaking. As someone who feels responsible for the future of this great application, I am creating a Flash Optimization series that will help educate the Flash development community on how to keep their Flash elements as lean as possible.

Flash Player 9 introduced two events (Event.ACTIVATE and Event.DEACTIVATE) that can assist you in fine-tuning your application so that it uses the fewest CPU cycles possible. These events allow you to detect when Flash Player gains or loses focus. As a result, code can be optimized to react to context changes.

Note: Use Event.ACTIVATE and Event.DEACTIVATE events to detect background inactivity and optimize your
application appropriately.

The following code listens to both events and dynamically changes the frame rate to zero when the application loses its focus. For example, the animation can lose focus when the user switches to another tab or puts the application into the background:


var originalFrameRate:uint = stage.frameRate;
var standbyFrameRate:uint = 0;

stage.addEventListener ( Event.ACTIVATE, onActivate );
stage.addEventListener ( Event.ACTIVATE, onDeactivate );

function onActivate ( e:Event ):void
{
// restore original frame rate
stage.frameRate = originalFrameRate;
}

function onDeactivate ( e:Event ):void
{
// set frame rate to 0
stage.frameRate = standbyFrameRate;
}

When the application gains focus again, the frame rate is reset to its original value. Instead of changing the frame rate dynamically, you could also consider making other optimizations, such as freezing and unfreezing objects. The activate and deactivate events allow you to implement a similar mechanism to the “Pause and Resume” feature sometimes found on mobile devices and Net books.