Writing text
Now that we have our font loaded, we can use the Text
class to print it to the screen. The Text
class is nothing more than an entity that can assemble words and sentences from small one-letter sprites.
Adding text to the scene is very straightforward. Add a new private
field for the score text and change the populate()
method of the GameScene
class as follows:
private Text scoreText; @Override public void populate() { createBackground(); createPlayer(); scoreText = new Text(16, 784, res.font, "0123456789", new TextOptions(HorizontalAlign.LEFT), vbom); scoreText.setAnchorCenter(0, 1); attachChild(scoreText); }
This will add the text 01234567890 to the top-left corner, at the position (16, 784) of the screen. The Text
class allows you to specify the horizontal alignment of the text with the three obvious choices: left, center, and right. If we have not used the prepareLetters()
method before, this constructor would create the letters for us too.
We are using...