I’m attempting to type out a string. When it reaches a predefined sequence it will prevent that sequence from being typed. Here’s what I have and why I think concat is my only solution.
The original code works like this:
function typewriter(myTextBox:TextField, string:String, speed:int):void
{
//This gets the length of the string
var stringLength:Number = string.length;
//A counter
var counter:Number = 1;
function wait()
{
//If the end of the string is reached, stop timer
if (counter == stringLength)
{
clearInterval(myTimer);
}
//check for predefined sequence
var currentTxt = string.substr(counter,2);
if (currentTxt == "/1"){
clearInterval(myTimer);
}
//Display current text
myTextBox.text = string.substr(0,counter);
counter++;
}
//Start timer
var myTimer = setInterval(wait, speed);
}
//Call the function to make typewriter effect
var Phrase:String = "This/1 That"
typewriter(myTextBox, Phrase, 200);
Running this lets me know the target string was found and ends the typing after “This”.
if I change line 19 to:
19 counter++; counter++;
This results in everything almost typing normally. I realize the problem is with line 23. The entire string is redrawn in myTextBox from 0 to the current counter. So regardless of skipping the counter ahead, it’ll still suddenly show everything. If you run this again you’ll notice “s/1” typed all at the same time.
I have already thought about using .replace but that will find and replace EVERY instance of “/1” wouldn’t it? I want this to work multiple times in a single string. So instead I try this on line 23:
23 myTextBox.text = string.substr(counter,1);
This results in single letters displaying “h,i,s, ,T,h,a,t” I noticed it skipped a letter at the start, but easily fixed by adding a space to the string in line 32. However, the “/1” was skipped over as intended!
So now I’m stuck wondering how can I concat these results to get “This That” to display in the text field? I’m not very AS3 literate, I can think up some long-winded solution that would probably work, but I’m looking for a simple answer.