Reputation: 441
After doing some reading, I've learned that multithreading is not possible in AS3, and I still can't get my head wrapped around the idea of "faking it". However, I'm working on creating a dialog conversation between two characters and thought about a way to do it that I'm a little skeptical about. So correct me if it's not how I should be doing it >:).
I added an Event.ENTER_FRAME listener that calls a function called "update".
Then to begin the conversation, I created a MouseEvent.CLICK listener that every time a user clicks on it, it would set a boolean called "moreDialog" to true.
Now the "moreDialog" is set to true, inside the "update" method that's being called every frame, there is an if loop that checks to see if moreDialog is true, if it is, then it will substring a word and put it inside a textField, and every frame after that it'll make the substring bigger by 1 until the word(s) has completely displayed to give it the visual affect of letters showing as if they're speaking.
Is this the best way to handle that? It does what I want, but I'm unsure if it may have performance issue or any other issues along the way as I begin to add more features. If it isn't, what would be the best alternative? Thanks in advance.
Upvotes: 0
Views: 468
Reputation: 6349
Your question is not very clear and it doesn't really sound like you're looking for anything related to multithreading.
If your goal here is to add text incrementally to a TextField....
There are different tweener classes such as TweenMax or Tweener but you could do this just as easily with a Timer
Using a timer you could use something like this
private var myTextString:String = "The string you want to display 1 char at a time";
private function myButtonClickHandler(e:MouseEvent):void{
if(!timer.running){ //so you don't click twice
var timer:Timer = new Timer(1000, myTextString.length); //1000 is the delay (in ms), myString.length is the amout of times we want it to fire (once or every letter)
timer.addEventListener(TimerEvent.TIMER, timerTick);
timer.start();
}
}
This assumes you have a text filed named myTextField
private function timerTick(e:TimerEvent):void{
myTextField.text += myTextString.charAt(e.currentTarget.currentCount) //using the current count to add the next char to your text field
}
You're also going to want to reset this method using a timerComplete handler
Upvotes: 2