簡體   English   中英

C#2010 Express,獲取並返回

[英]C# 2010 Express, get and return

我現在正在“ XNA 4.0游戲開發實例”一書中學習有關使用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

回答您的主要問題

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

這是一個只讀屬性。 當您嘗試通過調用YourClass.TilesPerRow來訪問它時,它將執行代碼塊中的代碼並返回該值。

get稱為Accessor 如MSDN所述,還有一個訪問器

讀取屬性后,將執行get訪問器的代碼塊; 當為屬性分配新值時,將執行set訪問器的代碼塊。 沒有設置訪問器的屬性被視為只讀。 沒有獲取訪問器的屬性被認為是只寫的。 具有兩個訪問器的屬性是可讀寫的。

由於該屬性沒有set您無法為該屬性賦值使其變為只讀。

這是MSDN屬性指南:

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

在您的情況下,它是將紙張的總寬度除以瓷磚的寬度。 這導致可以放置在一行中的圖塊總數(顧名思義)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM