Text rendering is the process of displaying text on the stage, usually in ways more flexible than the use of the say block or stage monitors.

There are many ways this can be achieved, including:

  • Using a set of sprite costumes to display images of letters, either cloned or stamped.
  • Using pen lines to draw letters.
  • Using pen dots to make a bitmap image or dot matrix.

Stamped Costume Method

An example of stamped text.
An example of symbols in a text sprite.

The examples in this section use the Stamp block. It is possible to achieve the same visual effect by creating clones: Simply replace each Stamp block with a Create Clone of (myself) block. However, the use of clones has two disadvantages:

  1. Creating and managing clones is prone to lag.
  2. A maximum of 300 clones can be displayed simultaneously.

Setup

Firstly, obtain or create costumes for each character. Each costume should be named the character it represents. Costume names are case sensitive so you can have a distinct character for "A" and "a", for example. If rendering spaces between words is needed, don't forget to also create an empty costume named with a space.

Script

define render text (text) at x: (text x) y: (text y)
go to x: (text x) y: (text y) // move to text start position
set [i v] to [1] // counter
repeat (length of (text)) // loop over the characters in the string
  switch costume to (letter (i) of (text))
  stamp // leave an image of this letter
  change x by (10) // move to next letter position
  change [i v] by (1) // increment counter to go to the next character
end
Note Note: Set the custom block to "run without screen refresh" to draw all the text instantly.

The script assumes all the characters have the same width. If not, see "Varying Letter Widths" below.

Varying Character Widths

This section builds on the tutorials above.

To accomplish this, create a list to store the widths: (character widths::list). For each costume, enter its width measurement into the list. Add 1 or 2px to the number so the characters can have small gaps between each other. Make sure that the costume number corresponds with the list number!

In the text rendering script, replace:

change x by (10)

With:

change x by (item (costume [number v]) of [character widths v])

To test that all of the letter widths are correct, it can be helpful to print out every letter and character supported as well as some sentences.

Symbols

Text sprites aren't limited to letters, numbers and a few special characters. Text sprites might also have symbols.

Pen Drawing Method

This is a complicated method requiring the shape of each character to be stored in a list as a series of strokes. Case sensing is often handled with costumes so there is some similarity with the stamped method above.

An example of pen text.

Centered Text

To create centered text, first determine the total width of the text to be rendered.

For characters that are all the same width, the total width can simply be calculated by multiplying it with the number of characters to draw:

go to x: ((text x::custom-arg) - (((length of (text::custom-arg)) * (10)) / (2))) y: (text y::custom-arg) // start position of text

For characters with varying widths, the total width can be calculated by looping over the letters before drawing, and summing the widths of the characters and spaces. The text can then be drawn with an offset calculated as half the width.

define render text (text) at x: (text x) y: (text y)
set [total width v] to [0]
set [i v] to [1]
repeat (length of (text))
  switch costume to (letter (i) of (text))
  change [total width v] by (. . .::gray) // add the width of the character
  change [i v] by (1)
end
go to x: ((text x) - ((total width) / (2))) y: (text y) // start position of text
... // now render the text

Multi-line Text

Text can be drawn in many lines. A simple way of achieving this is with a symbol that instructs the renderer to move to the next line instead of drawing it. Scratch blocks can't detect the newline character so choose a different character not used in the text, such as | or ~.

if <(letter (i) of (text::custom-arg)) = [|]> then
  set x to (text x::custom-arg) // go to the start
  change y by (-15) // move down by the line height
else
  ... // draw other characters here
end
change [i v] by (1)

Word Wrapping

Text that is too long for 1 line can be "wrapped" into the next line. This can be achieved by checking if the next-rendered word can fit in the remaining width. Words are typically split by spaces. When a space is encountered, look ahead over the next word until another space is found.

See Also

Cookies help us deliver our services. By using our services, you agree to our use of cookies.