Skip to content

Examples

Tommy Ettinger edited this page May 24, 2022 · 1 revision

Basic Example:

TypingLabels are a drop-in replacement for regular Scene2D Labels, so all you need is to change your Labels to TypingLabels and the library will be working already.

// BEFORE
Label label = new Label("Hello world!", skin);
stage.add(label);

// AFTER
TypingLabel label = new TypingLabel("Hello world!", skin);
stage.add(label);

Using Tokens:

You can also use Tokens to modify the text's behavior. See the Tokens page to learn more about them.

// Create some text with tokens
String text = "[GREEN]Hello,{WAIT} world!"
    + "[ORANGE]{SLOWER} Did you know orange is my favorite color?";

// Create a TypingLabel instance with your custom text
TypingLabel label = new TypingLabel(text, skin);

// Add the actor to any widget like you normally would
stage.add(label);

Using Variables:

Variable tokens are replaced by any values you assign at runtime. e.g. you can create a text Hello {VAR=name}! and set the variable value with label.setVariable("name", "Mr. Bob").

// Create some text with tokens
String text = "Would you like to buy {VAR=item} for only {VAR=price}?";

// Create a TypingLabel instance with your custom text
TypingLabel label = new TypingLabel(text, skin);

// Optionally assign variables to replace {VAR} tokens
label.setVariable("item", "an Obsidian Sword");
label.setVariable("price", "25,000 coins");

// Add the actor to any widget like you normally would
stage.add(label);

Alternatively you can replace replace tokens by using a listener call if you need more flexibility:

String text = "Would you like to buy {VAR=item} for only {VAR=price}?";
TypingLabel label = new TypingLabel(text, skin);

// Create a TypingListener to listen to variable replacements
label.setTypingListener(new TypingAdapter() {
    public String replaceVariable (String variable)  {
        if("item".equals(variable)) return "an Obsidian Sword";
        if("price".equals(variable)) return "{SHAKE}25,000 coins{ENDSHAKE}";
        return "-- Unknown Variable --";
    }
});

stage.add(label);

It's also possible to have global variables, a good example would be the player's name:

// Register the global variable when your game starts.
TypingConfig.GLOBAL_VARS.put("player", "Bob");

// Create some text with tokens
String text = "Welcome back {VAR=player}, how are you doing?";

// Create a TypingLabel instance with your custom text
TypingLabel label = new TypingLabel(text, skin);

Listening to Events:

You can use event tokens to be notified when that exact part of the text is processed. Really handy if you want to spawn fire particles or play a sound when a certain word appears.

To use events, simply put an event token in the middle of the text and set a listener to the label.

// Create text with events
String text = "{SPEED=0.25}This is an {EVENT=event_name}example of "
    + "how to {EVENT=spawn_fire}fire {EVENT=play_sound}events!";

TypingLabel label = new TypingLabel(text, skin);

// Create a TypingListener or TypingAdapter to listen to events as they're fired
label.setTypingListener(new TypingAdapter() {
    public void event (String event)  {
        System.out.println("Received text event: " + event);
    }

    public void end () {
        System.out.println("This is called when the text reaches the end.");
    }
});
Clone this wiki locally