RSS Valid
XHTML Valid
CSS Valid

Event.DEACTIVATE doesn’t work in browser

Wednesday, April 2nd, 2008

Working on a game recently on a project for WWE, I came accross an issue that many users were unable to play the game because they had clicked on another window or app while the levels were loading. When you do so, focus is transferred to that application or window, so when they were trying to type keys to play the game, nothing would happen. They’d have to just click back in the flash area. While most players would stay focused on the game, it became clear that this could be an issue for some users.

So, I set about setting up listeners for when the movie gained or lost focus, and depending on what stage the game was in (is it in gameplay mode, an options screen), pausing or removing timers and listeners (an overlay notifying the user that the flash application had lost focus):

// -- add listeners
private function addListeners():void {
	stage.addEventListener(Event.ACTIVATE, onActivate);
	stage.addEventlistener(Event.DEACTIVATE, onDeactivate);
}

// -- flash gains focus
private function onActivate(event:Event):void {
	// -- handle game re-playing here if paused
}

// -- flash loses focus
private function onDeactivate(event:Event):void {
	// -- handle game pausing here
}

In the flash player, it works brilliantly. You click something else, the game pauses. You click back in, it starts again. Lovely. Trouble is, once the flash application is in the browser, the Event.DEACTIVATE event does not behave as expected. The only time DEACTIVATE seems to be called is when you click the “URL location” entry field at the top of the browser (safari and firefox). So, if you were to click the desktop, the desktop now has keyboard focus, but flash does not receive notice that it has been deactivated.

Anyway, I’d love to hear if anyone has a workaround for this. Moock noted the bug a while back, but I haven’t seen any solutions (and he’d be the guy to fix it if it was fixable). I thought perhaps just “pausing” the game by default and then force the user to click in to activate it every time but that doesn’t solve the deactivate issue. Also, I experimented a bit with using javascript to check when the window loses focus (then notify flash via an ExternalInterface call) but it seemed to have the same issue.

On a side note, when writing a blog, it always seems to be a great idea to title your entries “______ doesn’t work.” Maybe it’s just me but that’s how I google for solutions every time.

One Response to "Event.DEACTIVATE doesn’t work in browser"

  1. M.LOVER says:
    April 17th, 2008 at 1:57 am

Leave a Reply

Availability

Ben is currently employed as the Technical Director at BKWLD in Seattle, Washington and is also available for freelance gigs. Please see the services and contact sections for more information or for work inquiries.