Home E-Books Special Effects and Game Development in Java Falling text (fallingtext)

Special Effects and Game Development in Java(TM) - Falling text (fallingtext)

by Anibal Wainstein

3.1.3 Falling text (fallingtext)  

Another interesting effect is the falling test effect. The word "falling" is a bit missleading, the text moves from the right and builds up the sentence or message letter by letter. Now, do a copy of the applet code in the last section. We begin by initializing the message in the init() method:



public void init()
{
    message="Falling text is a cool effect!"; }

It was nothing strange with that so we go on to the code in the run() method:

public void run()
{
    int L=message.length();
    //Create a string with a blankspace for the animation.
    String blankspace="                                 ";
    blankspace=blankspace+blankspace+blankspace;
    //Set the variable "mL" to the blankspace's length.
    int mL=blankspace.length();
    //The first loop will count up to the message's //length minus one.
    for (int i=0; i<L-1; i++)
    {
        //The second loop will count up to the blankspace's //length with 8 step's at the time in order //to increase speed.
        for (int j=0; j<mL; j+=8)
        {
            //Show an increasing part of the message //with a decreasing part of the blankspace //and the last letter in the message string.
            showStatus(message.substring(0,i)
                +blankspace.substring(j)
                +message.substring(i,i+1));
            try {Thread.sleep(50);}
            catch(InterruptedException e) {}
        }
    }
    //The last loop will see to that the //message is not overwritten by other status //messages.
    while(true)
    {
        showStatus(message);
        try {Thread.sleep(100);}         catch(InterruptedException e) {}
    } }

This effect looks much more complicated than the ones we have done until now. What you may be frowning towards are the nested FOR loops. The first loop is used to build up the sentence. For every new letter that is added in the sentence you must animate the following letter. This is what the inner loop is used for. There you use the variable "j" to crop down the blankspace area between the sentence and the animated falling letter. Please note that to crop the blankspace area we have used an overloaded substring() method, which takes a substring from the j-position to the end of the string. When the animation is finished, then we make sure we hold the thread and keep it working in a loop. This loop will be writing out the finished message in the status window, so that it is not overwritten by other browser messages. The best way to know how the effect looks like, is to see it in work by clicking here.

 

 


Next Page >>