Posted on September 5th, 2009Rasmus Wriedt Larsen5 comments
Ever tried making a text apear by making each letter coming on the screen one by one, and ended up with something likes this?
Read on and find out how to fix it!
We’re going to start by making a new .fla file, then make a dynamic texfield with multiline set to true and call that textF. I made a background for mine, so it ends up looking like this:
Lets start by making the normal animation that only works okay
Lets start off by making the basic setup with:
- a variable holding the text (when that is set in init() we remove the text we can see on the screen)
- a veariable telling us how far we are in the text right now
- an init funtion (init is just an abbreviation for initialize) that will reset the text and how far we have got in the text, and add a event listener.
- a loop executed for every frame (my framerate is 12)
Now this will just go on for ever, when howFarInTheText reaches a higher value than there are letters, it will just add a blank letter. So lets add a function removing the event listener:
The reason we need to subtract 1 from the length is that the first letter will come at allTheText.charAt(0), this mean it is zero-based. The length parameter of the string is not zero based, therefore we need to substract it by 1.
Here is it for a test (i know the framerate is real low, don’t worry, just read on – and you probably have to press the restart button by now)
So this pretty much works great. But there is one thing that I’m not happy with. We “waste” one fram typing all the spaces in the text. That doesn’t look too god to me – this was also the reason I made the framerate so low, else it’s harder to see. Here is how to fix it (add it to the end of the masterLoop function)
You can check it out here (press the restart button): (Either JavaScript is not active or you are using an old version of Adobe Flash Player. Please install the newest Flash Player.)
The problem is this:
First we see this:
The next frame we see this:
So the text jumps down a line and that just looks crappy.
So here is how we fix it:
The thing we’re going to do is split the text up in lines, so we will only write the letters from one line at one time. When we finished putting all the letters from one line, we go to the next one.
I’ve just put the final code for the good way of doing it here, as it’s basicly the same principles beeing used. This time we just need to go though an array (containing each line) to get our letters:
and here we have a nice result (press the restart button): (Either JavaScript is not active or you are using an old version of Adobe Flash Player. Please install the newest Flash Player.)
Maybe I’m being noobish about this, or i just looked over it, but isn’t the button’s MC supposed to be linkaged to the class you wrote, instead of flash.display.MovieClip?
Because this way it isn’t working for me…
Maybe i just did something horribly wrong, but i just downloaded your source, and that didn’t work either for me…
I don’t really think I understand your question, and I have a suspicion it’s because you don’t fully grasp the concepts of AS3. I would recommend reading Michael James William’s avoider game tutorial. If you’re still facing problems after this, just send me a mail and we’ll figure it out :)
Mushyrulez September 6th, 2009 at 04:23