• Home
tudwaythecore.com
blog
  • Recent Posts

    • Detecting focus in flash player
    • BitmapData PerlinNoise
    • Creating bridges in APE part 2
    • Creating bridges in APE
    • SteerWheels 2
    • Creating a simple APE example
    • Design me some levels!
    • Finished Migrating
  • Recent Comments

    • tudway on Creating a simple APE example
    • Ellen on Creating a simple APE example
    • tudway on Detecting focus in flash player
    • Markel on Detecting focus in flash player
    • Shawn on Sound in flash cs3 part 2 – pause
  • Categories

    • APE
    • flash
    • game
    • platform
    • sound
    • tutorial
    • Uncategorized
  • Archives

    • May 2009
    • October 2008
    • September 2008
    • August 2008
    • May 2008
    • April 2008
    • March 2008
  •  

    May 2009
    M T W T F S S
    « Oct    
     123
    45678910
    11121314151617
    18192021222324
    25262728293031
  • games

    • Game Showcase
May 29

Detecting focus in flash player

flash, game, tutorial Add comments

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.

Get Adobe Flash player

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.

PLAIN TEXT
Actionscript:
  1. var p:Boolean=false;
  2. detectFocus(p);
  3. function detectFocus(pausevar:Boolean) {
  4. var focusm:MovieClip = new MovieClip();
  5. focusm.graphics.beginFill(0x000000, 0.6);
  6. focusm.graphics.drawRect(0,0,550,400);
  7. focusm.graphics.endFill();
  8. addChild(focusm)
  9. stage.addEventListener(Event.DEACTIVATE, notinfocus);
  10. stage.addEventListener(Event.ACTIVATE, infocus);
  11. function notinfocus(event:Event):void {
  12. pausevar=true;
  13. focusm.visible = true
  14. }
  15. function infocus(event:Event):void {
  16. pausevar=false;
  17. focusm.visible = false
  18. }
  19. }

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!

Get Adobe Flash player

Click inside to gain focus and then arrow keys to move. (you can't win yet)

Works well doesn't it!

2 Responses to “Detecting focus in flash player”

  1. Markel Says:
    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

  2. tudway Says:
    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.

Leave a Reply

Powered by WordPress .::. Designed by SiteGround Web Hosting

cssandhtml