Приглашаем посетить
Культура (niv.ru)

Section 18.4.  Animation

Previous
Table of Contents
Next

18.4. Animation

Adding animation to your Flash movies is both fun and taxing. The key to animation is the SWFDisplayItem object returned by the add( ) function of your movie object. SWFDisplayItem objects have a variety of functions that allow you to move, rotate, scale, and skew your objects easily. This next example demonstrates some basic animation:

    $font = new SWFFont("Impact.fdb");
    $text = new SWFText( );
    $text->setFont($font);
    $text->moveTo(300, 500);
    $text->setColor(0, 0xff, 0);
    $text->setHeight(200);
    $text->addString("Text is surprisingly easy");

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

    $displayitem = $movie->add($text);

    for($i = 0; $i < 100; ++$i) {
            $displayitem->rotate(-1);
            $displayitem->scale(1.01, 1.01);
            $movie->nextFrame( );
    }

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

Although that code is largely the same as a previous script, the $movie->add($text) line has now changed so that the return value is captured and stored in $displayitem.

The script then runs through a loop 100 times, each time calling rotate( ), scale( ), and nextFrame( ). Animation works by defining the initial state of the movie, advancing the frame, then specifying changes from the previous frame. In practice, this means you use nextFrame( ) each time you want to move forward to the next frame of your Flash animation.

The rotate( ) function takes a single parameter, which is the floating-point value of the amount to rotate your SWFDisplayItem object from its current rotation. In our example, I have used -1, which means it adds -1 of a degree of rotation with each frame. Because of the way Flash rotation works, this means that the text rotates in a clockwise manner.

The scale( ) function takes two parameters: the amount to scale the object's width and the amount to scale its height. Again, this is based on its last state, which means that the scaling is compounded. By adding 0.01% to the size of our text over 100 frames, we are almost tripling the size of the object.

So, the contents of the for loop translate to "Rotate slightly, scale slightly, next frame" 100 times.


Previous
Table of Contents
Next