C# 2010 Экспресс, получить и вернуть

Сейчас я изучаю TileMaping с помощью Gemstone Hunter в книге "Разработка игр на XNA 4.0 на примере". На странице 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

1 ответ

Решение

Чтобы ответить на ваш главный вопрос

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

Это свойство только для чтения. Когда вы пытаетесь получить к нему доступ, позвонив YourClass.TilesPerRow он выполняет код в блоке и возвращает это значение.

get называется Accessor. Существует также набор доступа, как описано в MSDN

Блок кода для метода доступа get выполняется при чтении свойства; блок кода для установленного метода доступа выполняется, когда свойству присваивается новое значение. Свойство без установленного метода доступа считается доступным только для чтения. Свойство без метода доступа get считается доступным только для записи. Свойство, которое имеет оба метода доступа - это чтение и запись.

Поскольку собственность не имеет set Вы не можете присвоить значение этому свойству, делая его доступным только для чтения.

Вот руководство MSDN для свойств:

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

В вашем случае это деление общей ширины листа на ширину плитки. Это приводит к общему количеству плиток, которые могут быть размещены в ряд (как следует из названия).

Другие вопросы по тегам