ares_games
ares_games

Reputation: 1067

XNA property Game.IsActive is not working

I have recently noticed that sometimes the XNA property Game.IsActive does not always correctly reflect the state of the game window.

More specifically, Game.IsActive = true even if the game window is clearly not active (e.g. the top window bar is colored as inactive). I can reproduce this behaviour with an active Firefox window slightly overlapping the game window.

What could be the issue here?

As requested, this picture shows the problem: enter image description here

The game window is in the background, the browser (showing Stack Overflow) is in the foreground (and active), yet the property Game.IsActive is true (as you see in the visual studio output (magenta "circle") which is being written out every Update().

Could it be a problem, that I create a static reference of the XNA class Game in my core game class and use that?

Upvotes: 4

Views: 1136

Answers (1)

kara
kara

Reputation: 3455

As mentioned in the comments, the IsActive-Property has it's weaknesses.

You should subscribe to the events Game.Activated and Game.Deactivated. With an additional focus to your window on startup, you'll be fine.

IMHO: I don't like such "magic" properties like IsActive. Events are much more precise..

Example:

public class MyGame : Game
{
   public MyGame()
   {
      // Should move to Initialize-method..
      this.Activated += ActivateMyGame;
      this.Deactivated += DeactivateMyGame;
      this.IAmActive = false;
      // do you init-stuff

      // bring you window to front. After this point the "Game.IsActive"
      // will be set correctly, while IAmActive is correct from the beginning.
   }

   public bool IAmActive { get; set; }

   public void ActivateMyGame(object sendet, EventArgs args)
   {
      IAmActive = true;
   }

   public void DeactivateMyGame(object sendet, EventArgs args)
   {
      IAmActive = false;
   }
}

Everytime the Game gets or looses focus the methods ActivateMyGame and DeactivateMyGame are called. The Property IAmActive is false by default, which is the difference to IsActive.

Upvotes: 3

Related Questions