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
.
Note
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.