2

私は C# / XNA Game Studio を使用してアイソメトリック マップの描画に取り組んできました。

マップを描画するためのコードは次のとおりです。

public void Draw(SpriteBatch theBatch, int drawX, int drawY)
    {

        if ((drawY % 2 == 0))
            theBatch.Draw(tileTexture, new Rectangle((drawX * width), (drawY * length / 2), width, length), Color.White);
        else
            theBatch.Draw(tileTexture, new Rectangle(((drawX * width) + (width / 2)), (drawY * length / 2), width, length), Color.White);
    }

このメソッド内のコードは、ネストされた for ループ内にあるかのように動作し、左から右、上から下に描画します。y 値が奇数の場合、行は収まるようにシフトされますが、少しずれているように見えます。

これは、8x5 マップの生成された出力です。

http://imgur.com/xpDRU.png

ご覧のとおり、正しく表示されていません。コード内の数学の問題なのか、それともすべてが描画される順序に問題があるのか​​ どうかはわかりません。私は非常に新しいですC# に移行し、スプライトを操作するので、どんな助けでも大歓迎です。

役に立つかもしれないので、マップを描画するコードの他の関連部分を次に示します。

タイル クラス全体:

public class Tile
{
    // Dimension variables
    int height;
    int width;
    int length;

    String type;
    Texture2D tileTexture;
    Vector2 coordinates;

    /// 
    /// Tile Constructor
    /// 
    public Tile(ContentManager theContent, String theType, Vector2 theCoordinates)
    {
        width = 68;
        length = 46;

        type = theType;
        coordinates = theCoordinates;

        // Sets the right texture to the texture ref
        if (theType == "grass")
            tileTexture = theContent.Load<Texture2D>(@"Tiles\iso_grass");
    }

    /// 
    ///  Draws the tile at the given location
    /// 
    public void Draw(SpriteBatch theBatch, int drawX, int drawY)
    {

        if ((drawY % 2 == 0))
            theBatch.Draw(tileTexture, new Rectangle((drawX * width), (drawY * length / 2), width, length), Color.White);
        else
            theBatch.Draw(tileTexture, new Rectangle(((drawX * width) + (width / 2)), (drawY * length / 2), width, length), Color.White);
    }


}

1 行のタイルを保持する TileRow クラス。

public class TileRow
{
        public List<Tile> Row = new List<Tile>();
        public int rowLength;

        public TileRow(int theLength, int yIndex, ContentManager theContent)
        {
            rowLength = theLength;
            Tile thisTile;

            // Here the tiles are created and added to the row
            for (int x = 0; x < rowLength; x++)
            {
                thisTile = new Tile(theContent, "grass", new Vector2(x, yIndex));
                Row.Add(thisTile);
            }
        }

        /// 
        /// Draw -- invokes the draw method of each tile in the row
        /// 
        public void DrawRow(SpriteBatch theBatch, int currentY)
        {
            for (int x = 0; x < rowLength; x++)
            {
                Row[x].Draw(theBatch, currentY, x);
            }
        }
    }
}

すべての行を保持する MapStruct クラス

public class MapStruct
{

    public List<TileRow> allRows = new List<TileRow>();
    int depth;

    // Constructor
    public MapStruct(ContentManager theContent, int theDepth, int rowLength)
    {
        depth = theDepth;
        TileRow thisRow;

        // Here we make a row of tiles for each level of depth
        for (int y = 0; y < depth; y++)
        {
            thisRow = new TileRow(rowLength, depth, theContent);
            allRows.Add(thisRow);
        }
    }

    ///
    /// Draw - this method invokes the draw method in each tile row, which then draws each tile
    ///
    public void DrawMap(SpriteBatch theBatch)
    {
        for (int y = 0; y < depth; y++)
        {
            allRows[y].DrawRow(theBatch, y);
        }
    }
}

この問題を修正する方法についてのヘルプと、コードを改善する方法についてのアドバイスをいただければ幸いです。

4

2 に答える 2

1

ループが各行の Y に少しずつ追加されているようです。Tile 関数でこの変数を見つけました。

length = 46;

確認していませんが、「長さ」はタイルの高さですか?その場合は、少し調整してみてください。おそらく、タイルの高さを引くのを忘れたのでしょう。したがって、タイルの一辺が 6 ピクセルの場合、オフセットの長さは pr. 行はわずか 40 です。

また、奥行きを錯覚させるために、カメラに最も近いタイルを最後にプロットする必要があるため、上から下にプロットすることを忘れないでください。

于 2010-12-23T02:12:34.753 に答える
0

BerggreenDK は正しいと思いますが、あなたのコメントは彼の答えを誤解しているように見えます。タイルは、緑色の表面領域の「画面の高さ」のみを含む Y オフセットで描画する必要があります。したがって、フル タイル サイズを描画しますが、長さ - 5 で行をオフセットします (各行は距離の半分だけオフセットする必要があるため、10 は推定オフセットであり、5 はその半分です)。

theBatch.Draw(tileTexture, new Rectangle((drawX * width), (drawY * (length) / 2), width, length - 5), Color.White);

...パラメータが正しければ。

また、行は後ろから前に描く必要があるのに、左から右に描くように見えますか? 私は XNA を知らないので、それを修正するコードを表示することはできません...よく見ると、「遠くにある」列のいくつかのタイルが「近い」タイルによって過剰に描画されています。私は XNA を知らず、描画順序がどのように決定されるかを理解していないため、その修正を提供することはできません...

于 2010-12-23T03:54:27.100 に答える