1

私の主な問題は、生成されたタイルの位置を取得する方法や、マウスがどこにあるかを知る方法がわからないことです。マウスなどを検出するために衝突を使用する必要がありますか?コードを最適化し、位置などを簡単に取得できるようにするためにできることはありますか

テクスチャのロードなど、コードからいくつかのことを取り除いて、問題の一部ではないため、テクスチャを短くしました。

私のタイル生成コード

    public Block[] tiles = new Block[3];
    public int width, height;
    public int[,] index;
    public Rectangle tileRect;

public void Load(ContentManager content)
    {
        tiles[0] = new Block { Type = BlockType.Grass, Position = Vector2.Zero, texture = grass};
        tiles[1] = new Block { Type = BlockType.Dirt, Position = Vector2.Zero, texture = dirt};

        width = 50;
        height = 50;

        index = new int[width, height];

        Random rand = new Random();
        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                index[x,y] = rand.Next(0,2);
            }
        }
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                    spriteBatch.Draw(tiles[index[x,y]].texture, tileRect = new Rectangle(x * 64, y * 64, 64, 64), 
                        Color.White);          
            }
        }  
    }

ブロックプロパティコード

public enum BlockType
{
    Dirt,
    Grass,
    Selection
}

public class Block
{
    public BlockType Type { get; set; }
    public Vector2 Position { get; set; }
    public Texture2D texture { get; set; }
}
4

2 に答える 2

1

このコードでうまくいくはずです。このメソッドをタイル生成クラスに追加できます。(未テスト)

public bool IsMouseInsideTile(int x, int y)
{
    MouseState MS = Mouse.GetState();
    return (MS.X >= x * 64 && MS.X <= (x + 1) * 64 &&
        MS.Y >= y * 64 && MS.Y <= (y + 1) * 64);
}

この関数は、ニーズに合わせて編集できます。

編集: このコードについて少し説明します。

  • Mouse.GetState()マウスの現在の位置を次のように取得しますVector2

  • あなたの描画方法が言うように、タイル[a ,b]はその位置にあります[a * 64, b * 64]

  • [(a + 1) * 64, (b + 1) * 64]テクスチャは64 by 64 pixels寸法であるため、タイルの最大x座標とy座標はになります。

  • マウスがすべてのタイルの中にあるかどうかを確認します。必要に応じて、MouseHoverイベントを追加できます。

その他の編集: あなたのコメントに基づいて、私のコードを編集しました。

さらに多くの編集: Drawメソッドのコードは次のとおりです。

public void Draw(SpriteBatch spriteBatch)
    {
        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                    spriteBatch.Draw(tiles[index[x,y]].texture, tileRect = new Rectangle(x * 64, y * 64, 64, 64), 
                        Color.White);
                    if(IsMouseInsideTile(x, y))
                        spriteBatch.Draw(selected.texture, tileRect = new Rectangle(x * 64, y * 64, 64, 64), 
                                        Color.White);
            }
        }  
    }
于 2012-03-29T14:03:02.897 に答える
1

XNA Update関数では、Mouse.GetState()を使用してマウスの位置を取得できます。これにより、マウスのX座標とY座標のプロパティが得られます。それらをタイルサイズで割り、切り捨て(床)して、最も近いタイル座標インデックスを取得します。

追加されたコード

public static Vector2 GetGridCoordinates(MouseState mouseState, int gridSize){
    return new Vector2(
        (int)Math.Floor(mouseState.X / gridSize),
        (int)Math.Floor(mouseState.Y / gridSize)
    );
}

おそらくこれをMouseStateクラスの拡張関数にすることもできるので、あなたがしなければならないのは次のようなものだけです。

Vector2 gridCoords = Mouse.GetState().GetGridCoordinates(MyGridSize);

しかし、私はおそらくそれを考えすぎています...

于 2012-03-29T08:08:55.077 に答える