1

我现在正在“XNA 4.0 Game Development by Example”一书中使用 Gemstone Hunter 学习 TileMaping。在第 299 页上,它讲述了它正在做什么,但现在每个方法正在做什么。我有一些问题,但主要的是,get & return 有什么作用?:

我不是要求任何人来解决它,而是在做什么?

我也想知道 tileSheet 在做什么。

我想了解 MapWidth 和 MapHeight。

(我正在尝试写评论以了解每件作品的作用)

#region Declarations
    //TileWidth, and TileHeight are the size that each tile will be when playing and editing the game.
    public const int TileWidth = 48;
    public const int TileHeight = 48;
    //MapWidth and MapHeight do... I don't know.
    public const int MapWidth = 160;
    public const int MapHeight = 12;
    //MapLyaers represent the three back grounds in the MapSquare class.
    public const int MapLayers = 3;
    //skyTile is the blue tile that will be on the background, or the 
    private const int skyTile = 2;
    //MapSquare organizes the tile sheet into map cells by width and height. 
    static private MapSquare[,] mapCells =
        new MapSquare[MapWidth, MapHeight];

    //Tells the the game if its playing or editing the maps.
    public static bool EditorMode = true;

    public static SpriteFont spriteFont;
    static private Texture2D tileSheet;
    #endregion

    #region Initialization
    //The Initialize() method establishes all of the MapCells as MapSquares with empty tiles on each layer.
    //On back ground skyTile (2) will be the blue background, 0 will be transparent.
    static public void Initialize(Texture2D tileTexture)
    {
        tileSheet = tileTexture;

        for (int x = 0; x < MapWidth; x++)
        {
            for (int y = 0; y < MapHeight; y++)
            {
                for (int z = 0; z < MapLayers; z++)
                {
                    mapCells[x, y] = new MapSquare(skyTile, 0, 0, "", true);
                }
            }
        }
    }
    #endregion

    #region Tile and Tile Sheet Handling
    public static int TilesPerRow
    {
        get { return tileSheet.Width / TileWidth; }
    }

    public static Rectangle TileSourceRectangle(int tileIndex)
    {
        return new Rectangle(
            (tileIndex % TilesPerRow) * TileWidth,
            (tileIndex / TilesPerRow) * TileHeight,
            TileWidth,
            TileHeight);
    }
    #endregion
4

1 回答 1

2

回答你的主要问题

#region Tile and Tile Sheet Handling
public static int TilesPerRow
{
    get { return tileSheet.Width / TileWidth; }
}

这是一个只读属性。当您尝试通过调用YourClass.TilesPerRow它来访问它时,它会执行块中的代码并返回该值。

get被称为访问器。正如 MSDN 所解释的,还有一个 set 访问器

get 访问器的代码块在读取属性时执行;set 访问器的代码块在属性被分配一个新值时执行。没有 set 访问器的属性被认为是只读的。没有 get 访问器的属性被认为是只写的。具有两个访问器的属性是可读写的。

因为该属性没有,所以set您不能为此属性赋值,使其成为只读的。

这是 MSDN 属性指南:

http://msdn.microsoft.com/en-us/library/vstudio/w86s7x04.aspx

在您的情况下,它将工作表的总宽度除以瓷砖的宽度。这导致可以放置在一行中的瓷砖总数(顾名思义)。

于 2013-11-08T15:27:56.813 回答