#!/usr/bin/perl -w # # Example 9-8. Assemble a skeletal framework of Sprites use strict; use SWF qw(:ALL); use XML::Parser; # Used to parse the XML input file my $filename = ''; # The output filename my $sprites = 0; # The number of tags so far my $m = undef; # The top-level Movie my $item = undef; # The current DisplayItem SWF::useSWFVersion(5); my $parser = new XML::Parser(Handlers => { Start => \&start_tag, End => \&end_tag, }); if (defined $ARGV[0]) { $parser->parsefile($ARGV[0]); # Parse the input XML } else { die "Please provide the name of an XML file.\n"; } sub start_tag { my $expat = shift; # This is an Expat XML parser object # that we don't use here. my $tag = shift; # The tag name my %a = @_; # The tag's attributes # Check the tag type if ($tag eq 'movie') { $m = new SWF::Movie; # Create a new Movie if ($a{'width'} && $a{'height'}) { $m->setDimension($a{'width'}, $a{'height'}); } } elsif ($tag eq 'sprite') { if ($m) { $item = new_sprite($a{'url'}); # Create a new (empty) Sprite # loads the external movie clip } } elsif ($tag eq 'moveTo') { if ($item) { $item->moveTo($a{'x'}, $a{'y'}); # Move the current item } } elsif ($tag eq 'scaleTo') { if ($item) { $item->scaleTo($a{'x'}, $a{'y'}); # Scale the current item } } elsif ($tag eq 'rotateTo') { if ($item) { $item->rotateTo($a{'degrees'}); # Rotate the current item } } elsif ($tag eq 'remove') { if ($item) { $item->remove(); # Remove the current item $item = undef; } } elsif ($tag eq 'nextFrame') { if ($m) { $m->nextFrame(); # Go to the next frame } } } sub end_tag { my ($expat, $tag) = @_; # The Expat object and the tag name if ($tag eq 'movie') { $m->output(); } elsif ($tag eq 'sprite') { $item = undef; # The sprite is no longer the current item } } sub new_sprite { my $url = shift; # The URL of the external movie clip to load $sprites++; # Increment number of sprites my $sprite = new SWF::Sprite(); $sprite->nextFrame(); my $i = $m->add($sprite); $i->setName("sprite$sprites"); $m->add(new SWF::Action("loadMovie('$url','sprite$sprites');")); return $i; }