【问题标题】:C# 2010 Express, get and returnC# 2010 Express,获取和返回
【发布时间】:2013-11-08 15:19:09
【问题描述】:

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

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

我也想知道 tileSheet 在做什么。

我想了解 MapWidth 和 MapHeight。

(我正在尝试编写 cmets 以了解每个部分的作用)

#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

【问题讨论】:

  • 您应该先从一本初级 C# 书开始,然后再拿起那本书。
  • 我在上课,如果我收到了这本书,去年我们已经学习了 C++ 的基础知识并制作了一个基于文本的游戏,我了解了基础知识,但是这里面还有一些东西在 C++ 之外,这个类不是最好的,但正在到达那里。我在网上寻找简单的帮助,但有很多不同的方法可以用 get 做事,我使用 2010 年,其他人使用其他年份,女巫可能与我有不同和混乱。
  • 简短的回答是它们都返回值/对象。过去this C# tutorial,它可能有用。那里有很多有用的教程。
  • 我明白 get 是在装腔作势 {return tileSheet.Width / TileWidth;} 我可以做 TilesPerRow = tileSheet.Width / TileWidth;
  • 在这种特殊情况下,TilePerRow 是一个没有支持成员的属性。每当您想使用TilesPerRow 属性时,您必须先显式执行TilesPerRow = tileSheet.Width / TileWidth;。当前构造封装了该属性的计算。

标签: c# xna get return gemstone


【解决方案1】:

回答你的主要问题

#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

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

【讨论】:

    猜你喜欢
    • 2011-06-30
    • 2015-12-10
    • 2018-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多