`

Sunday, September 19, 2010

Detecting focus on Flash using AS3

Sometimes it is needed to tell whether the user is focused on the Flash or not... in this tutorial, you will learn how to do that using AS3.

We need to create 2 listeners - one for focusing in, and one for focusing out. These events are called Event.ACTIVATE and Event.DEACTIVATE.

stage.addEventListener(Event.ACTIVATE,  inFocus);
stage.addEventListener(Event.DEACTIVATE, outFocus);

Now we need to show the user that he is focused out or in. Usually, this toggles the pause menu in Flash game, so that if a person focuses on something else, he will see that the game is paused and then proceed to click it to focus on it.

here is the example code:

function inFocus(event:Event){

     ball_mc.play()

}
function outFocus(event:Event){

     ball_mc.stop();

}


Preview

Download .zip

No comments: