官术网_书友最值得收藏!

Time for action – reading textures into memory

  1. Double-click on Game1.vb in Solution Explorer to open it or bring it to the front if it is already open.
  2. In the Class Declarations area of Game1 (right below Private WithEvents spriteBatch As SpriteBatch), add:
    Private playingPieces As Texture2D
    Private background As Texture2D
    Private titleScreen As Texture2D
  3. Add code to load each of the Texture2D objects at the end of LoadContent():
    playingPieces = Content.Load(Of Texture2D)("Textures\Tile_Sheet")
    background = Content.Load(Of Texture2D)("Textures\Background")
    titleScreen = Content.Load(Of Texture2D)("Textures\TitleScreen")

What just happened?

In order to load the textures from disk, you need an in-memory object to hold them. These are declared as instances of the Texture2D class.

A default XNA project sets up the Content instance of the ContentManager class for you automatically. The Content object's Load() method is used to read .XNB files from disk and into the Texture2D instances declared earlier.

One thing to note here is that the Load() method requires a type identifier, specified in the first set of parenthesis using the Of notation, before the normal parameter list. Known in .Net programming as a Generic, many classes and methods support this kind of type specification to allow code to operate on a variety of data types. We will make more extensive use of Generics later when we need to store lists of objects in memory. The Load() method is used not only for textures, but also for all other kinds of content (sounds, 3D models, fonts, and so on) as well. It is important to let the Load() method know what kind of data you are reading, so that it knows what kind of object to return.

主站蜘蛛池模板: 陕西省| 日喀则市| 潮州市| 习水县| 沽源县| 交城县| 德清县| 宁海县| 舒城县| 上饶市| 南城县| 仪征市| 城步| 广南县| 旺苍县| 乐陵市| 准格尔旗| 涿州市| 宁陵县| 枣强县| 阆中市| 手机| 汶上县| 留坝县| 罗源县| 广元市| 云龙县| 溧水县| 尼勒克县| 沅陵县| 宜阳县| 七台河市| 桐庐县| 两当县| 武邑县| 靖江市| 临沂市| 华亭县| 科尔| 靖远县| 石狮市|