Приглашаем посетить
Техника (find-info.ru)

Section 18.2.  Flash Text

Previous
Table of Contents
Next

18.2. Flash Text

Following the rest of the library, text inside your Flash movie is manipulated using objects. The two key classes here are SWFFont and SWFText . The former holds the actual font shape data, whereas the latter holds information about the text as a whole, including color, position, string data, and the instance of SWFFont used to draw the letters.

The code to generate text works differently under Windows and Unix. First up, Linux users:

    $font = new SWFFont("Impact.fdb");
    $text = new SWFText( );

    $text->setFont($font);
    $text->moveTo(200, 400);
    $text->setColor(0, 0xff, 0);
    $text->setHeight(200);
    $text->addString("Text is surprisingly easy");

    $movie = new SWFMovie( );
    $movie->setDimension(6400, 4800);
    $movie->add($text);

    header('Content-type: application/x-shockwave-flash');
    $movie->output( );

The Windows code isn't far off, and the end result is the same:

    $font = new SWFFont("Impact");
    $text = new SWFTextField( ); // new!
    $sprite = new SWFSprite( ); // new!

    $text->setFont($font);
    $text->setColor(0, 0xff, 0);
    $text->setHeight(200);
    $text->addString("Windows is a little harder!");

    $spritepos = $sprite->add($text); // new!
    $spritepos->moveTo(200, 400); // new!

    $movie = new SWFMovie( );
    $movie->setDimension(6400, 4800);
    $movie->add($text);

    header('Content-type: application/x-shockwave-flash');
    $movie->output( );

You'll need to alter your HTML file to display the new script, and also change the width and height attributes of the <embed> object so that the Flash movie is larger; otherwise, you will find the text is probably too small to notice.

That code starts with the two new classes, SWFFont and SWFText. The SWFFont class is remarkably easy to usemerely pass the name of the FDB file you want to use as a font, and save the return value for later use. You can create your own FDB fonts using Ming's makefdb utility (available from Ming's home page, http://ming.sourceforge.net), so you should replace Impact.fdb in the example with your own font.

In line two of our script, we create a new SWFText object and store it in a $text variable. This object works in pretty much the same way as our previous SWFShape objectwe set various properties of it, then add it to the parent movie once we're done.

The first thing we do with our $text object is call its setFont( ) function, which makes this SWFText object render in the font used to create the SWFFont object specified as the only parameter. In our case, we created our SWFFont object using Impact.fdb, so calling setFont( ) using the new SWFText object will draw the text in this object using the Impact font.

Next, we call the moveTo( ) function to place the text neatly inside the movie, and then call the setColor( ) function (the values are in hexadecimal) to set the text to lime green. The setHeight( ) function sets the height of the text in twips, but again, remember that the final size of the text is dependent on the size at which the movie is played back, and also the dimensions of the parent movie object itselfthe value you set here is just relative to the rest of the movie.

The most important function we call for our SWFText object is addString( )this allows us to draw the string passed as parameter one to the position we set with our moveTo( ) call. It is important to note that the pen the text is drawn with is set to the baseline. If you use moveTo( ) to set the position to 0,0, the text drawn will be drawn outside of your movie.


Previous
Table of Contents
Next