One of the biggest advantages to Ming is that it is object-oriented - you create a shape object, tell it what colour it should be, then add it to the movie. In order to try to cram as much in as possible this month -- Flash-generation code can be very long -- we're going to dive right into some code using some simple shapes, then pick it apart straight after. If your knowledge of object-orientation is rusty or non-existent, you might find it helpful to read the chapter on Objects.
<?php $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 code as ming1.php. 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="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">
First we create a new instance of the SWFMovie class and assign it to our $mov variable. An SWFMovie object allows you to manipulate attributes of the movie as a whole - size, colour, animation frame rate, etc. It is also used to add other Flash objects to your movie, so it is essential you hold onto the SWFMovie object created.
SetDimension() 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. Unsurprisingly, we use objects of this class to manipulate shapes in Flash movies - the process is simply create, manipulate, 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. AddFill() 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 parameters - the 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 edge - in our example above, this is red.
Next we call MovePenTo() and DrawLineTo() several times. MovePenTo() lifts the drawing "pen" from the canvas and places it down at the X and Y points specified by the first two parameters respectively. DrawLineTo() 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. DrawLineTo() 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 parameter - this 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 very 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.
To view your animation in action, load the HTML page you created earlier into your browser - all being well you should see something like the picture below. If your Flash movie does not load at all, it is generally the result of an error in the PHP script. However, when viewing the HTML page you will not see any PHP warnings, because the Flash movie is being sent direct 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 browser by visiting http://yourserver/path/to/ming1.php - you should see the errors printed as normal.
Copyright ©2015 Paul Hudson. Follow me: @twostraws.