2016-04-22 9 views
0

私はどのように私のグリッドの位置にmonogame/XNAの私のマウスの位置を変換することができません。私が達成しようとしているのは、基本的にグリッド内の特定のセル上の矩形の色を、クリックすると別の色に変更することです。グリッド上のマウスの位置を取得

これは私の初めてのグリッドシステムであり、実際にこれを行う方法の手掛かりはありません。

グリッドの作成方法は次のとおりです。ここで基本的に何が起こるかは、最初にグリッド内のすべてのセルに0を入力することです。そして、Game1クラスのupdateメソッドでどのような値を割り当てるかによって、矩形の色が変わります。

ありがとうございました!

public int[,] gridCell; 
    Texture2D texture; 
    public Rectangle rect; 
    int col; 
    int row; 
    const int gridSize = 32; 



    //Initializes the constructor 
    public Grid(int sizeCol, int sizeRow) 
    { 
     //texture = sprite; 
     col = sizeCol; 
     row = sizeRow; 
     gridCell = new int[col, row]; 

     for(int i = 0; i<col;i++) 
     { 
      for(int j = 0; j<row;j++) 
      { 
       gridCell[i, j] = 0; 
      } 
     } 
    } 


    public void Draw(SpriteBatch spritebatch) 
    { 
     for (int i = 0; i <= col-1; i++) 
     { 
      for (int j = 0; j <= row-1; j++) 
      { 
       if (gridCell[i, j] == 0) 
       { 
        spritebatch.FillRectangle(i * 32, j * 32, 31, 31, Color.CornflowerBlue, 0f); 
        spritebatch.DrawRectangle(new Vector2(i * 32, j * 32), new Vector2(32, 32), Color.Black, 1f); 
       } 

       else if(gridCell[i,j] == 1) 
       { 
        spritebatch.FillRectangle(i * 32, j * 32, 31, 31, Color.Yellow, 0f); 
        spritebatch.DrawRectangle(new Vector2(i * 32, j * 32), new Vector2(32, 32), Color.Black, 1f); 
       } 
       else if (gridCell[i, j] == 2) 
       { 
        spritebatch.FillRectangle(i * 32, j * 32, 31, 31, Color.Red, 0f); 
        spritebatch.DrawRectangle(new Vector2(i * 32, j * 32), new Vector2(32, 32), Color.Black, 1f); 
       } 
       else if (gridCell[i, j] == 3) 
       { 
        spritebatch.FillRectangle(i * 32, j * 32, 31, 31, Color.Purple, 0f); 
        spritebatch.DrawRectangle(new Vector2(i * 32, j * 32), new Vector2(32, 32), Color.Black, 1f); 
       } 
       else if (gridCell[i, j] == 4) 
       { 
        spritebatch.FillRectangle(i * 32, j * 32, 31, 31, Color.Blue, 0f); 
        spritebatch.DrawRectangle(new Vector2(i * 32, j * 32), new Vector2(32, 32), Color.Black, 1f); 
       } 
       else if (gridCell[i, j] == 5) 
       { 
        spritebatch.FillRectangle(i * 32, j * 32, 31, 31, Color.Black, 0f); 
        spritebatch.DrawRectangle(new Vector2(i * 32, j * 32), new Vector2(32, 32), Color.Black, 1f); 
       } 
      } 
     } 
    } 
+0

をチェック私はMonoGameと過度に慣れていないんだけど、あなたはこのゲームの世界(あなたのグリッドにマウスカーソルの位置を変換する必要があるかもしれません)座標系は、他のゲームエンジンとかなり共通しているので、そのことを念頭に置いてください。マウスの位置がグリッドセルの矩形内にあるか、グリッドセルの矩形と衝突しているかどうかをテストする必要があります。これを行うには、[Rectangle.Contains Method](https://msdn.microsoft.com/en-us/library/microsoft.xna.framework.rectangle.contains.aspx)を参照してください。次に、グリッドセルに対してマウスのX/Y座標をテストできます。 –

+0

"グリッド内のその特定のセルの矩形の色を変更する"が、セルのグリッド全体に対して1つの矩形しかありませんか?なぜ、各セルに1つの矩形を割り当てて、それらの矩形を描画しないのですか? – LibertyLocked

答えて

2

[OK]を次に示します。グリッドの1つの正方形が8x8であるとします。マウスの位置は8と0の間で、0/8/16/24 /にする必要があります。マウスの位置を8で分けると(Mpositionは4x20となります)、0.5x2 .5、それに8を掛けると、再び4x20になります。BUT:除算結果を切り捨てると、0x2が得られ、8を乗算した値が0x16になるので、(0x16)はマウスが入っている四角形、および0x2はマウスの行列位置です。

public Vector2 PositionByGrid (int gridSize) 
    { 
     Vector2 result = new Vector2(MouseState.GetState().X, MouseState.GetState().Y); 
     result.X = (int)(result.X/gridSize) * gridSize; 
     result.Y = (int)(result.Y/gridSize) * gridSize; 
     return result; 
    } 

この関数は、正方形の位置を返します。あなたは行列内の位置をしたい場合は、単に代わりにresult.X = (int)(result.X/gridSize);を入れresult.X = (int)(result.X/gridSize) * gridSize;

EDIT: @Slubberdegullionが示唆したように
あなたのグリッドが正方形でない場合は、ここでの関数であるが、行列が適用されるために同じ原理(長方形で

public Vector2 PositionByGrid (int gridWidth, int gridHeight) 
    { 
     Vector2 result = new Vector2(MouseState.GetState().X, MouseState.GetState().Y); 
     result.X = (int)(result.X/gridWidth) * gridWidth; 
     result.Y = (int)(result.Y/gridHeight) * gridHeight; 
     return result; 
    } 



EDIT 2::) これがまた提案
である、あなたはを短縮することができ機能、この(提案NO2ので説明)のように見えるように:

public void Draw(SpriteBatch spritebatch) 
{ 
    for (int i = 0; i <= col-1; i++) 
    { 
     for (int j = 0; j <= row-1; j++) 
     { 
      Color newColor; 
      switch(gridCell[i, j]) 
      { 
       case 0: newColor = Color.CornflowerBlue; break; 
       case 1: newColor = Color.Yellow; break; 
       case 2: newColor = Color.Red; break; 
       case 3: newColor = Color.Purple; break; 
       case 4: newColor = Color.Blue; break; 
       case 5: newColor = Color.Black; break; 
       default: newColor = Color.White; break; 
      } 
      spritebatch.FillRectangle(i * 32, j * 32, 31, 31, newColor, 0f); 
      spritebatch.DrawRectangle(new Vector2(i * 32, j * 32), new Vector2(32, 32), Color.Black, 1f); 

     } 
    } 
} 

色がVector4のbasicalyあるので、これはゲームに4つの新しい整数変数を追加することに等しいもう少しRAMを(使用することは事実ですが、 floatの代わりにint)とCPU時間が必要ですが、ゲームの完全性と可読性のためにかかるコストは非常に低いです。
提案No2:あなたのコードから、私はあなたが初心者であることを知っています。そして、言われたように、コードの同じ行(グループ)を繰り返さないようにあなたに告げることです。代わりに、関数を作成する/私はあなたのDraw関数/などで何をしましたか。あなたの方法は、1つのものを変更する必要がある場合は、Xの場所で変更する必要があります。そして、それをもう一度、あるいは何回か変更しなければならないのはどうですか?反復コードを1つの関数に保持することによって、必要が生じたときには、その関数内で1つだけ変更する必要があります。

+1

幅と高さが同じ場合に機能します。それ以外の場合は、gridSizeを適切な次元に置き換えます。あなたが2つのうちのどれを受け入れるべきか疑問に思っているなら、これは受け入れられた答えでなければなりません。 – Slubberdegullion

+0

すごい、これは私が必要としていた以上のものです。これで私を助けてくれてありがとう! – Favonius

0

グリッドを構成するときに、グリッドの衝突矩形の配列を生成します。

Rectangle[,] gridRects = new Rectangle[row, col]; 

for (int i = 0; i < row; i++) 
{ 
    for (int j = 0; j < col; j++) 
    { 
     gridRects[i,j] = new Rectangle(i * gridSize, j * gridSize, gridSize, gridSize); 
    } 
} 

は、次にフレームごとに、Updateで、衝突

MouseState ms = Mouse.GetState(); 

for (int i = 0; i < row; i++) 
{ 
    for (int j = 0; j < col; j++) 
    { 
     if (gridRects.Contains(ms.X, ms.Y)) 
     { 
      // Mouse is on gridCell[i,j] 
     } 
    } 
} 
関連する問題