#!/usr/bin/perl -w # # Example 9-2. A grid of red squares collapses in on itself, # then expands to its original state. use strict; use SWF qw(Movie Shape DisplayItem); SWF::setScale(1.0); # Define a grid my $grid = 8; my ($w, $h) = ($grid*100, $grid*100); # Create a square my $s = new SWF::Shape(); $s->setLineStyle(1, 255, 0, 0); $s->movePenTo(0,0); $s->drawLineTo(0, 50); $s->drawLineTo(50, 50); $s->drawLineTo(50, 0); $s->drawLineTo(0, 0); # The displayList array holds the DisplayList objects as they are placed onstage my @displayList = (); my $m = new SWF::Movie(); $m->setDimension($w, $h); # Place a grid of squares on the stage and store the reference to each DisplayItem foreach my $i (0..$grid-1) { foreach my $j (0..$grid-1) { my $item = $m->add($s); $item->moveTo($i*100, $j*100); push @displayList, $item; } } # Now create 30 frames; in each frame, move each square # 1/30th of the way toward the center of the grid, and rotate # the square 360/30 degrees. Then repeat the same thing in the # opposite direction, ending up where we started. my $frames = 30; my ($cx, $cy) = ($w/2, $h/2); foreach my $direction (1, -1) { # 1 =in, -1 = out foreach my $f (1..$frames) { foreach my $i (0..$grid-1) { foreach my $j (0..$grid-1) { $displayList[$i*$grid+$j]->move( $direction*(int($cx-$i*100)/$frames), $direction*(int($cy-$j*100)/$frames)); $displayList[$i*$grid+$j]->rotate(360/$frames); } } $m->nextFrame(); } } # Create the SWF file $m->save("example9-2.swf");