Oct 16, 2008

AS3 Optimization Tip

Creating a site that has a lot of transitions is fun. One that uses a ParallaxManager to manage and scroll 6 layers of objects is also fun.

What isn't fun is having the user leave the site to check mail, work, oh, I don't know, do something else and have the site continually scroll. In steps 'Event.MOUSE_LEAVE'.

The only drawback is that one cannot effectively test this in the IDE. It does, however, work in the browser.

This might also be expanded to do a little house cleaning while the user isn't using the site. Once you have access to 'stage', add the listener. Then use the functions to stop and start the site. I've left out the project specific code and what you see here is bare bones. MOUSE_MOVE only happens when the user is actually on the stage so that is how we re-activate things. Thanks go out to Kirupa.


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

// followed by:
private function _onLeaveStage($evt:Event):void
{
// disable site code
stage.addEventListener(MouseEvent.MOUSE_MOVE, _onReturnToStage, false, 0, true);
};

private function _onReturnToStage($evt:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, _onReturnToStage);
//re-enable site code
};

Share this Post


                           

Comments


hebchop     Nov 10, 2009
I've been doing the mouse move / leave thing for a while, but I think ACTIVATE might be the way to go as well.

Philip     Apr 26, 2009
That means that the site will stop (or whatever) whenever the user moves their mouse over the browser chrome, even if the site is still visible right?
@Nick, maybe Event.ACTIVATE and Event.DEACTIVATE would be more effective, not tested it yet tho ;)

Michael Vestergaard     Dec 12, 2008
Great tip. I've used this a couple of times and it works great :-)

Mikko     Nov 14, 2008
Thanks for sharing this. I used the MOUSE_LEAVE event to remove an ENTER_FRAME loop of Papervision render engine. CPU usage drops from 50% to almost 0% when mouse leaves the flash area.

Cheers guys!

pallzoltan     Nov 04, 2008
i did a similar thing on a page we were doing, and if i remember correctly, i added a MouseEvent.ROLL_OVER listener to the stage to know when the user is back - its a bit more simple than the MOUSE_MOVE way.

cheers

Nick     Oct 27, 2008
I think is better using the events ACTIVATE, dispatched when Flash Player or an AIR application gains operating system focus and becomes active, and DEACTIVATE, dispatched when Flash Player or an AIR application loses operating system focus and is becoming inactive.

Cheers

Skye     Oct 16, 2008
Good call. I should add this to our agency site (http://www.cannonballagency.com/), which also has some scrolling parallax going on. Although the scrolling stops when momentum runs out, ENTER_FRAME listeners are consuming CPU cycles (at least when an overlay is not open). This would be a good way to reduce them in the scenario you mention. Cheers.


Speak






Submit »