Home > OS >  How do you pause between text in this code?
How do you pause between text in this code?

Time:09-17

I'm trying to put a pause between "Long ago," and what comes after that but I'm having trouble trying to do it. How could I make it work? I've attempted to use a timer but I'm not very good at doing that which made it hard to implement it.

Here is my code:

var snd: textStory = new textStory();
var snd2: soundStory = new soundStory();
var myString: String = "Long ago, two races\nruled over Earth:\nHUMANS and MONSTERS.";
var myArray: Array = myString.split("");
snd2.play();
addEventListener(Event.ENTER_FRAME, frameLooper);
function frameLooper(event: Event): void {
    if (myArray.length > 0) {
        if (n == 1) {
            tf.appendText(myArray.shift());
            n = 0;
            snd.play();
        } else {
            n  ;
        }
    } else {
        removeEventListener(Event.ENTER_FRAME, frameLooper);
    }
}

CodePudding user response:

Something like that, I think. It is a data-driven solution where you prepare a proper data input so that your main loop (ENTER_FRAME handler in your case) won't have to think too much and just process one data entry at once.

var E:Array = [""];

// It is considered a good tone to name classes
// starting with uppercase: TextStory, SoundStory.
var snd: textStory = new textStory();
var snd2: soundStory = new soundStory();

var laString:String = "Long ago";
var myString:String = ", two races\nruled over Earth:\nHUMANS and MONSTERS.";

var myArray:Array = new Array;

// Here we form an Array that starts with "Long ago"
// then 20 empty entries to skip, then the rest.
myArray = myArray.concat(tocharArray(laString));
myArray = myArray.concat(emptySpaces(20));
myArray = myArray.concat(tocharArray(myString));

snd2.play();

addEventListener(Event.ENTER_FRAME, frameLooper);
function frameLooper(event: Event): void
{
    if (myArray.length < 1)
    {
        removeEventListener(Event.ENTER_FRAME, frameLooper);
        return;
    }
    
    var aChar:String = myArray.shift();
    
    // Just skip a frame if there's an empty character.
    if (aChar == "")
    {
        return;
    }
    
    tf.appendText(aChar);
    
    // Make no typing sound on space characters... I guess?
    if (aChar != " ")
    {
        snd.play();
    }
}

// Returns an Array ["", "", "", ... , ""] of the given length.
function emptySpaces(length:int):Array
{
    // This doubles E as many times as needed
    // to allow us to make a slice long enough. 
    while (E.length < length)
    {
        E = E.concat(E);
    }
    
    // Returns a portion of E of the given length.
    return E.slice(0, length);
}

// Converts the given String into an Array
// of characters and empty "" entries.
function tocharArray(value:String):Array
{
    var result:Array = new Array;
    
    for (var i:int = 0; i < value.length; i  )
    {
        // You can put more or less "" here to make the
        // typing loop skip more than a single
        // frame after typing a character.
        result.push(value.charAt(i), "", "");
    }
    
    return result;
}
  • Related