ニュース速報XNA360

How to: ゲームの終了

最終更新:

匿名ユーザー

- view
だれでも歓迎! 編集
この例では、実行中のアップデート処理を強制終了させて、ゲームを終わらせる方法を示します。

アップデートハンドラに残っている処理を無視して、強引にゲームループを抜ける為に


1. Microsoft.XNA.Framework.Gameクラスから派生してください。 

using Microsoft.XNA.Framework;

class SimpleGameLoop : Game {
}

2. ESCキーの状態がKeyboardState.IsKeyDownであるかをチェックする
    メソッド(以下、終了チェックメソッド)を作成してください。

    もし、ESCキーが押されていたなら、上記の終了チェックメソッド内で、
    Game.Exitメソッドを呼び、そのメソッドの戻り値としてtrueを返して
    ください。

   bool exitKeyPressed()
   {
       // Check to see if ESC was pressed on the keyboard
       // or Back was pressed on the controller.
       if (keyboardState.IsKeyDown( Keys.Escape ) ||
           gamePadState.Buttons.Back == ButtonState.Pressed)
       {
           this.Exit();
           return true;
       }
       return false;
   }

3. Updateメソッドをオーバーライドし、その中で上記の終了チェック
   メソッドを呼び、その戻り値がtrueでない場合(falseの場合)のみ、
   Game.Updateメソッドを呼びだしてください。

   protected override void Update(GameTime gameTime)
   {
       gamePadState = GamePad.GetState(PlayerIndex.One);
       keyboardState = Keyboard.GetState();

       // If the ESC key is pressed, skip the rest of the update.
       if (exitKeyPressed() == false)
       {
           base.Update(gameTime);

           // 以下にゲームのメインとなるロジックを記述してください。
       }
   }

4. Game.Exitingイベントを処理するイベントハンドラを作成してください。

    Exitingイベントは、Game.Exitメソッドが処理完了直前に発行します。

   public SimpleGameLoop()
   {
       graphics = new GraphicsDeviceManager(this);

       // イベントハンドラを作成する
       this.Exiting += new EventHandler(SimpleGameLoop_Exiting);
   }

   // このメソッドは、Exitingイベント発生時にコールバックされます。
   void SimpleGameLoop_Exiting(object sender, EventArgs e)
   {
       // ゲームが終わる前に実行しなければならない全ての処理は、以下で行ってください。
   }

C#

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;

class SimpleGameLoop : Game
{
   private GraphicsDeviceManager graphics;
   private GamePadState gamePadState;
   private KeyboardState keyboardState;

   protected override void Draw(GameTime gameTime)
   {
       graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
       base.Draw(gameTime);

       // Add game-specific drawing code.
   }

   protected override void Update(GameTime gameTime)
   {
       gamePadState = GamePad.GetState(PlayerIndex.One);
       keyboardState = Keyboard.GetState();

       // If the ESC key is pressed, skip the rest of the update.
       if (exitKeyPressed() == false)
       {
           base.Update(gameTime);

           Simulate();
       }
   }

   void Simulate()
   {
       // Add game-specific logic.
   }

   bool exitKeyPressed()
   {
       // Check to see if ESC was pressed on the keyboard or Back was pressed on the controller.
       if (keyboardState.IsKeyDown( Keys.Escape ) || gamePadState.Buttons.Back == ButtonState.Pressed)
       {
           Exit();
           return true;
       }
       return false;
   }

   public SimpleGameLoop()
   {
       graphics = new GraphicsDeviceManager(this);
       this.Exiting += new EventHandler(SimpleGameLoop_Exiting);
   }

   void SimpleGameLoop_Exiting(object sender, EventArgs e)
   {
       // Add any code that must execute before the game ends.
   }


   static void Main( string[] args )
   {

       using (SimpleGameLoop game = new SimpleGameLoop())
       {
           game.Run();
       }

   }
}

タグ:

+ タグ編集
  • タグ:
ウィキ募集バナー