0

Window.ClientBounds.Width を使用して、スプライトがウィンドウの境界内にあるかどうかを確認しようとしています。これを Game1.cs 以外のクラスで使用したい。Car.cs クラスがあり、そのクラス内にウィンドウの境界内にあるかどうかを確認する独自の Update メソッドが必要だとしますが、これを Window.ClientBounds.Width を使用することはできません!? また、静的 int gameBorder = Window.ClientBounds.Width; を作成することもテストしました。Game1.cs 内でそのように値に到達しますが、これも機能しませんか?! ヘルプは貴重です!ありがとう!

無料の XNA の質問に対して、stackowerflow よりも優れた方法はありますか?

4

2 に答える 2

0

そのすべての作業に行き、そのすべてのメモリを浪費する必要はありません。

XNAには、オブジェクトの位置をテストするための非常に正確で具体的な手段があります。

GraphicsDeviceManagerのgraphics.PreferredBackBufferWidthメソッドとHeightメソッドを渡すだけで、ウィンドウの幅と高さを取得できます。

そこから、オブジェクトがそれらの位置の長方形内にあるかどうかに基づいて、オブジェクトがゲームウィンドウに表示されるかどうかがわかります。

したがって、バックバッファの幅と高さを640x480に設定するとします。

次に、テクスチャの境界がその長方形内にあるかどうかを確認するだけです。

だから、これがあなたの関数です:

public void CheckIfWithinWindow(int width, int height)
{
    Rectangle wndRect = new Rectangle(0, 0, width, height);
    Rectangle carRect = new Rectangle(carPos.X, carPos.Y, carTexture.Width, carTexture.Height);

    if (wndRect.Intersects(carRect))
    {
         //carTexture is within currently visible window bounds!
    }
    else
    {
         //carTexture is NOT within currently visible window bounds!
    }
}

次に、この関数を、開始XNAクラスのUpdateメソッドから次のように呼び出すことができます。

public void Update(GameTime gameTime)
{
     myCar.CheckIfWithinWindow(graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight);
}

お役に立てば幸いです。楽しむ。

于 2012-06-11T15:29:55.857 に答える
0

Car クラスを構築するとき、車が含まれているはずの Game への参照、または車が表示されるはずの GraphicsDevice への参照を渡します。

class Car
{
   // Keep a reference to the game inside the car class.
   Game game;

   public Car (Game game)
   { 
      this.game = game;
   }

   public void Update(.....
   { 
       // You can access the client bounds here.
       // the best thing about this method is that
       // if the bounds ever changes, you don't have
       // to notify the car, it always has the correct
       // values.
   }
}
于 2012-06-10T19:02:25.397 に答える