Skip to main content
Inspiring
December 3, 2018
Answered

Type-on using AS3 - user taps any key & scripted text appears

  • December 3, 2018
  • 1 reply
  • 434 views

i’m Wanting to be able to create type-on animations whereby no matter what key the user clicks on the keyboard (A thru Z, Space, Return) scripted text appears one character at a time.   (Note: by scripted I mean predetermined paragraph of text - not AS3 type script)

The idea being to simulate typing or data entry so that each key stroke animates an additional character in the string.

I’m new to AS3 and I’m unsure how to approach this task?

Thanks in advance for any advice you can land !

    This topic has been closed for replies.
    Correct answer ClayUUID

    Assuming you have a dynamic textfield on the stage named "myText":

    var myString:String = "this is a test";

    var chars:Number = 0;

    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler);

    function keyHandler(event:KeyboardEvent):void {

        if (++chars === myString.length) {

            stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyHandler);

            trace("DONE!");

        }

        myText.text = myString.substr(0, chars);

    }

    1 reply

    Joseph Labrecque
    Community Expert
    Community Expert
    December 3, 2018

    You could animate the text by using a mask - growing the mask along each frame of animation.

    Inspiring
    December 3, 2018

    Thank you for the suggestion.  I could also create each character as a separate image and script the movie to advance a frame on each keystroke but it’s labor intensive.

    I’m Hoping there’s a more eloquent solution using AS3 whereby the characters are stored in a string - say - and animate to the stage in a field as key strokes register.

    Thanks again for your input Joseph!

    Joseph Labrecque
    Community Expert
    Community Expert
    December 3, 2018

    For pure AS3, you could just keep appending a new character (stored in an Array, perhaps) to a textfield with each keystroke:

    TextField - Adobe ActionScript® 3 (AS3 ) API Reference