I am right now learning about TileMaping using Gemstone Hunter in the book "XNA 4.0 Game Development by Example". On page 299 It tells what it's doing but now what each method is doing. I have some questions but the main one is, what does get & return do?:

I am not asking for any one to solve it but what is get doing?

I would also like to know what tileSheet is doing.

I would like to know about MapWidth and MapHeight.

(I am trying to write comments to know what each piece does)

#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
有帮助吗?

解决方案

To answer your main question

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

This is a readonly property. When you try to access it by calling YourClass.TilesPerRow it executes the code in the block and returns that value.

The get is called an Accessor. There is also a set accessor as explained by MSDN

The code block for the get accessor is executed when the property is read; the code block for the set accessor is executed when the property is assigned a new value. A property without a set accessor is considered read-only. A property without a get accessor is considered write-only. A property that has both accessors is read-write.

Because the property has no set you cannot assign a value to this property making it readonly.

Here is the MSDN guide for properties:

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

In your case it is dividing the total width of the sheet by the width of a tile. This leads to the total number of tiles that can be placed in a row (as the name implies).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top