This article provides how to make the typewriter effect in Scratch.

Variables

First create these local Variables.

  • (i) - a variable used for iteration
  • (text shown) - the contents of the "say" message every iteration

Script

This is the script for the typewriter

define typewrite (text) how long? (sec) ask? (ask) speed: (speed)
set [text shown v] to [] 
set [i v] to [1] // Initialize vars
repeat (length of (text)) // We need to repeat to 'typewrite' every letter
set [text shown v] to (join (text shown) (letter (i) of (text))) // Will take the previous text shown and add the next letter.
say (text shown) // Display text
change [i v] by (1) // Iterate
wait ((1) / (speed)) secs // wait for a little bit, based on speed
end
if <(ask) = [yes]> then // Checks if it should ask the resulting text
ask (text) and wait // If so, asks text
else 
wait (sec) secs // If not, wait a little longer to read
say [] // Say nothing to 'delete' text off of page
end

This block can be used multiple times now.

Usage

The parameters to be filled in, in order, are:

  1. The text to be shown
  2. The number of seconds to wait after the typewriting is finished
  3. "yes" if the typewriting should end by asking the text as a question, leave it blank otherwise. (If this is "yes", leave the number of seconds to wait blank.)
  4. The number of characters per second to show. For example, if this is set to 5, every second 5 more characters will have appeared.
    Note Warning: Don't leave this blank, because otherwise the block will wait forever!