Section 18.1. A Simple Movie


18.1. A Simple Movie

One of the biggest advantages to Ming is that it is object-oriented, so you create a shape object, tell it what color it should be, then add it to the movie. The same process applies for all the other operations in Ming, which makes the code easy to read. Here is a script that creates a basic movie:

     $mov = new SWFMovie( );     $mov->setDimension(200,20);     $shape = new SWFShape( );     $shape->setLeftFill($shape->addFill(0xff, 0, 0));     $shape->movePenTo(0,0);     $shape->drawLineTo(199,0);     $shape->drawLineTo(199,19);     $shape->drawLineTo(0,19);     $shape->drawLineTo(0,0);     $mov->add($shape);     header('Content-type: application/x-shockwave-flash');     $mov->output( ); 

Save that script as ming1.php.

First we create a new instance of the SWFMovie class and assign it to our $movvariable. An SWFMovie object allows you to manipulate attributes of the movie as a whole size, color, animation frame rate, etc. It is also used to add other Flash objects to your movie, so you must hold on to the SWFMovie object that was created.

The setDimension( ) function is an SWFMovie function that allows you to set the height and width of a movie by specifying values in the first and second parameters. Remember that Flash movies generally have their dimensions set in their host application (usually a web browser). The values you specify here are for the movie as you are creating it; however, if the Flash movie is forced to display at a different size, your items will automatically be proportionally scaled to fit the assigned space.

Moving on to the core of the code, we have a new class: SWFShape. Not surprisingly, we use objects of this class to manipulate shapes in Flash moviesthe process is simply to create, manipulate, and then add to the parent movie object. If you forget to add your shapes to your movie object, the end result is that they'll be missing from the final output, so be careful.

In the example above, the parameter that SetLeftFill( ) takes is the return value of an AddFill( ) call. This is a function of the SWFShape class, and is overloaded (there is more than one version of it). The version used in the example above takes four parametersthe amount of red to use, the amount of blue, then green, and finally, an optional alpha parameter. The fill returned by the AddFill( ) function is used to supply the first parameter to SetLeftFill( ), which is also overloaded. The end result is that the value passed to SetLeftFill( ) sets the fill on the left-hand side of the edgein our example above, this is red.

Next we call MovePenTo( ) and DrawLineTo( ) several times. The movePenTo( ) function lifts the drawing "pen" from the canvas and places it down at the X and Y points specified by the first two parameters, respectively. The drawLineTo( ) function moves the pen in the same sort of way, except that it does not "lift" the pen from the canvas first, meaning that a line is drawn from the last pen location to the X and Y parameters passed into drawLineTo( ), respectively. The drawLineTo( ) function is called a total of four times, giving us a box, and finally we call the Add( ) function of our SWFMovie object, $mov, passing in our new box as the parameterthis adds the new shape to the final output.

The last two lines are crucial to the whole process, and must be used precisely as seen above. The first of the two calls the header( ) function, passing in the correct content type to instruct browsers that the information following is a Shockwave Flash movie. The last line calls the Output( ) function of our SWFMovie object, which sends all the information you have prepared about your Flash movie out to your client. Once you have called this line, your script is complete.

Generally speaking, you will want to embed your Flash movies inside web pages, and that requires inserting the following line somewhere in a HTML page:

     <embed src="/books/1/302/1/html/2/ming1.php" menu="false" quality="best" bgcolor="#FFFFFF"     swLiveConnect="FALSE" WIDTH="200" HEIGHT="200"     TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/     shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" /> 

To view your animation in action, load the HTML page into your browser. If your Flash movie does not load at all, there may be an error in the PHP script. When viewing the HTML page, you will not see any PHP warnings, because the Flash movie is being sent directly to your browser's Flash player as part of a larger page. You can work around this by loading the Flash movie directly into your browseryou should see the errors printed as normal.



PHP in a Nutshell
Ubuntu Unleashed
ISBN: 596100671
EAN: 2147483647
Year: 2003
Pages: 249

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net