Last updated
Was this helpful?
Last updated
Was this helpful?
The Text object provides functionality for drawing bitmap fonts in 2D and 3D space. It inherits from the class and shares many similarities with the class. Just as the class is associated with the , the Text object is associated with the . Note that most games which use Text do so through Gum instead of using the FlatRedBall Text object.
The FlatRedBall engine stores a default internally. Any Text object created will use this default . To create a Text object in code, add the following to your Screen's CustomInitialize, or Game1.cs Initialize after initializing FlatRedBall:
The Text object behaves the same as other FlatRedBall objects which have been added to their respective managers. This means that when you add a Text object, it will be drawn and updated automatically for you until it is removed.
A common mistake is to add and remove text every frame to change what it displays. Instead of doing this, you should set a text's DisplayText property to change what it says. For example:
For more information on the persistence of objects, see . For code example comparing the approach of creating a new text vs. setting the DisplayText, see the .
The DisplayText property allows for changing the string that a Text object renders. The following code sets the text depending on whether the user has a gamepad connected: Add the following include statements:
Add the following to the Initialize method after initializing FlatRedBallServices:
Text objects can be positioned just like any PositionedObject. For example, a text object's X and Y values can be set:
By default Text objects added through the FRB editor are sized to be pixel perfect given the game's resolution. The Text object provides a number of properties for changing size.
TextureScale controls the size of the text relative to its source texture. A TextureScale of 1 results in the text drawing pixel perfect if the game is running at 100% scale. If the game runs at larger scale, then Text using a fixed TextureScale will drawn larger.
The SetPixelPerfectScale function sets the text size to be pixel perfect given the argument camera or layer. The following code sets the Text to be pixel perfect to the main camera:
Note that pixel-perfect text will not appear zoomed if the game settings are zoomed.
The Text object provides three different variables for changing the size of text. These properties are:
Scale
Spacing
NewLineDistance
The following code creates three text objects which have non-default values for these three properties.
For more information on fields and properties, see the .
- Properties for changing a Text's color.
- Text implements the IAttachable interface.
Did this article leave any questions unanswered? Post any question in our for a rapid response.