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