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

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.

主站蜘蛛池模板: 镇宁| 周口市| 新和县| 策勒县| 扶余县| 北宁市| 拜城县| 松桃| 特克斯县| 博湖县| 石阡县| 南丹县| 临桂县| 南陵县| 五寨县| 平武县| 建始县| 德保县| 于都县| 北川| 长宁区| 景东| 肇庆市| 东乌珠穆沁旗| 得荣县| 甘南县| 东明县| 江孜县| 清徐县| 孟连| 西乌珠穆沁旗| 浦县| 宝山区| 通化市| 民县| 湛江市| 叶城县| 西吉县| 武邑县| 大邑县| 屏东市|