Have you ever wondered how games can detect if they are in focus? Well I'm going to show you anyway and it is a very useful function that i use in all my new projects.
I'm going to show you what i mean first.
There should be a grey rectangle above. If you click it, you gain focus in the flash file until you click away, eg outside the swf, in the browser address bar or even out of the browser. when focus is gained the swf will go white.
This is because when the swf is not in focus i am creating a transparent block over the entire screen. Imagine it as pausing a game when the focus is lost.
I have created a universal function with a pause variable in the parameter so that you can easily pause a game or app when the focus is lost.
-
var p:Boolean=false;
-
detectFocus(p);
-
function detectFocus(pausevar:Boolean) {
-
var focusm:MovieClip = new MovieClip();
-
focusm.graphics.beginFill(0x000000, 0.6);
-
focusm.graphics.drawRect(0,0,550,400);
-
focusm.graphics.endFill();
-
addChild(focusm)
-
stage.addEventListener(Event.DEACTIVATE, notinfocus);
-
stage.addEventListener(Event.ACTIVATE, infocus);
-
function notinfocus(event:Event):void {
-
pausevar=true;
-
focusm.visible = true
-
}
-
function infocus(event:Event):void {
-
pausevar=false;
-
focusm.visible = false
-
}
-
}
line 1: Creating the pause variable that the detectfocus function changes, this is so the function can pause your game.
line 2: Calling the detectfocus function using the pause variable p.
line 3: setting up the constructor for the function with 1 variable, pausevar.
lines 4 - 8: creating the rectangle that goes over the top of the screen, for more help on the drawing api try this post note the alpha of the fill colour is 0.6 so it is transparent.
line 9: Adding an event listener for when the swf is not in focus which calls the function notinfocus.
line 10: Adding an event listener for when the swf is in focus which calls the function infocus surprisingly enough!
lines 11 - 14: Creating the notinfocus function which sets the pause variable to true and also makes the rectangle visible.
lines 15 - 18: Creating the in focus function that does exactly the opposite to the not in focus function, so it sets pause to false and makes the rectangle invisible.
Now lets see it in action for one of the new levels for steerwheels!
Click inside to gain focus and then arrow keys to move. (you can't win yet)
Works well doesn't it!




November 7th, 2009 at 10:09 am
Hi sir, I just want to ask where can I get your detectFocus() class?
I really like it. Another is that how can I make a small screen that shows the ball when it is not in the screen.
I really appreciate hearing from you.
Thanks in advanced,
Markel
November 9th, 2009 at 8:25 pm
The detectFocus function is written in the script above the movie!
As for the screen thing, research bitmap data, the screen is actually a snapshot of the movieclip that holds all the objects in. this snapshot is taken everyframe and so overwrites the current one making it look like a video.
March 1st, 2010 at 2:03 pm
This is awesome. In combination with keypress detection it would make a good copy protection. For example, if you display images or text and you dont want them to be copied, you can stop the use of printscreen key detecting its keypress and replacing the frame with a blank one. If the user then goes outta focus to use it, you detect focus is lost and replace current frame with blank one anyways… one could detect ctr+p too in order to avoid print commands when in focus… I will have to test and see how it works out and if I can circumvent it somehow… maybe those plugins that download flash content would break this measure… but it remains to be tested yet…
April 26th, 2010 at 11:14 pm
very helpful thanks, I am creating a AS3 shooting game for fun and i needed a way to pause the game.
July 27th, 2010 at 11:24 pm
Do you know how to do a focus detect that starts/pauses when the swf file is simply on screen or not? I see banners do it on sites all the time — meaning the animation doesn’t start until you scroll down and actually see the swf banner on the screen. If it is off screen it will sit paused until you scroll down.
That make sense? Any tricks you know to do that or do you think it’s done in javascript page code instead?