コンポーネント、サービスの登録は基底のゲームクラスのコンストラクタで行う事
登録したサービスの受け取りはコンポーネントのイニシャライズメソッド内で行うとタイミング的に合う
グラフィックデバイスのビルドは基底ゲームのコンストラクタを抜けた後で完成する
つまりイニシャライズメソッド以降でデバイス関連の正常な値など拾える事に注意
又、カスタムクラスのリストなどは配列要素を作成するまではOKだが、内容物のコンストラクタの関係から
イニシャライズメソッド以降でADDするなど注意が必要である

<要点>
  • フレームワークに近い動作ほどコンストラクタ内で
  • カスタムクラスのインスタンスはイニシャライズメソッド以降で

<例>フレームワーク側
       public Game1()
       {
           graphics = new GraphicsDeviceManager(this);
           Content.RootDirectory = "Content";
           //XBOX360に合わせたバックバッファの作成
           graphics.PreferredBackBufferWidth = 1280;
           graphics.PreferredBackBufferHeight = 720;

           //コンポーネントの登録
           shooterGame = new ShooterGame(this);
           Components.Add(shooterGame);

           gameBalance = new GameBalance();
       }

コンポーネント側
   class ShooterGame : DrawableGameComponent
   {
       SpriteBatch spriteBatch;
       VisualProvider visualProvider;

       public ShooterGame(Game game)
           : base(game)
       {
       }

       public override void Initialize()
       {
           visualProvider = new VisualProvider();
           Game.Services.AddService(typeof(IVisualProvider), visualProvider);  //サービスの登録
           Rectangle clientBounds = Game.Window.ClientBounds;
           visualProvider.PlayerList.Add(new Player(Game, PlayerIndex.One, PlayerType.TypeA, new Vector2(500, 500), clientBounds));
           visualProvider.PlayerList.Add(new Player(Game, PlayerIndex.One, PlayerType.TypeB, new Vector2(700, 500), clientBounds));
           
           base.Initialize();
       }
最終更新:2012年07月31日 22:07