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

Time for action – creating a square texture

Add the following code snippet to the LoadContent() method:

letterFont = Content.Load<SpriteFont>("Segoe14");
playerSquare = Content.Load<Texture2D>("Square");

CheckForNewWord();

What just happened?

The default Content object can be used to load any type of asset from our content project into an appropriate instance in memory. The type identifier in angle brackets after the Load() method name identifies the type of content we will be loading, while the parameter passed to the Load() method specifies the asset name of the content.

Asset names can be set via the Properties window in Visual Studio, but would default to the name of the content file, path included, without an extension. Since all of the content objects will be translated into .xnb files by the content pipeline, there is no need to specify the format that the file was in before it was processed.

In our case, both of our content items are in the root of the content project's file structure. It is possible (and recommended) to create subdirectories to organize your content assets, in which case you would need to specify the relative path as part of the asset name. For example, if the Segoe14 sprite font was located in a folder off the root of the content project called Fonts, the default asset name would be Fonts\Segoe14.

Tip

Special characters in asset names

If you do organize your assets into folders (and you should!) your asset names will include the backslash character (\) in them. Because C# interprets this as an escape sequence in a string, we need to specify the name in the Content.Load() call as either "Fonts\\Segoe14" or @"Fonts\Segoe14". Two backslashes are treated as a single backslash by C#. Prefacing a string with the @ symbol lets C# know that we are not using escape sequences in the string so we can use single backslash characters. A string prefaced with the @ symbol is called a verbatim string literal.

The last thing our LoadContent() method does is call the (as yet undefined) checkForNewWord() method. We will construct this method towards the end of this chapter in order to generate a new word both at the beginning of the game and when the player has completed spelling the current word.

主站蜘蛛池模板: 新建县| 黄石市| 吴旗县| 宁南县| 桑植县| 凤台县| 阿勒泰市| 黄浦区| 西青区| 江华| 曲松县| 浦东新区| 平舆县| 峨边| 临沭县| 古蔺县| 贵定县| 临潭县| 延庆县| 西盟| 宁晋县| 兴安盟| 车致| 龙川县| 南阳市| 济宁市| 万宁市| 申扎县| 富源县| 三原县| 古蔺县| 庄河市| 阿合奇县| 青海省| 邵武市| 达日县| 金川县| 南安市| 南京市| 六盘水市| 宝清县|