Through its powerful ActionScript language, Flash provides a very flexible scripting environment to allow developers to take more direct control over the operation and flow of their script. For example, you can call stop() to stop playing the movie, then play() to continue; gotoFrame() allows you to jump to a particular part of your movie, and getURL() allows you to browse to a new web page. There is a large collection of actions available to you, and the PHP documentation has some very good (if very long) examples on how to make use of various functions.
In order to give you a quick start, I am going to take a look at a quick example of actions. Save this next piece of code as ming3.php and modify your HTML to point to the new file:
<?php
function MakeActionBox($red, $green, $blue){
$shape = new SWFShape();
$shape->setLeftFill($shape->addFill($red, $green, $blue));
$shape->movePenTo(-100,-20);
$shape->drawLineTo(100,-20);
$shape->drawLineTo(100,20);
$shape->drawLineTo(-100,20);
$shape->drawLineTo(-100,-20);
return $shape;
}
$button = new SWFButton();
$button->setUp(MakeActionBox(0xff, 0, 0));
$button->setOver(MakeActionBox(0xff, 0xff, 0));
$button->setDown(MakeActionBox(0, 0, 0xff));
$button->setHit(MakeActionBox(0, 0, 0));
$button->addAction(new SWFAction("getURL('http://www.slashdot.org', 'slashdot');"), SWFBUTTON_MOUSEUP);
$movie = new SWFMovie();
$movie->setDimension(200,200);
$displayitem = $movie->add($button);
$displayitem->moveTo(100,100);
header("Content-type: application/x-shockwave-flash");
$movie->output();
?>
The first thing you will notice is that I define a custom function, MakeActionBox, to handle some of the grunt work you will experience when working with the SWFButton class. The SWFButton class, which I make an instance of for our $button variable, has several "states" that each require a shape - how the button looks when it is up, when the mouse is over it, when the mouse is clicked on it, and where the mouse can be clicked on it.
Each of these states require a complete shape of their own, so I automate the process of setting up a shape by using the function MakeActionBox.
Going through the main chunk of code line by line, you can see the first thing I do is create an instance of SWFButton and file it away in the $button variable. I then call four functions - setUp(), setOver(), setDown(), and setHit() - to define how this button should look when the user interacts with it. My implementation is short to save space, but you will generally find it is more visually appealing to have more than just the colour change between states :)
Next we come to the important function of this particular script - addAction. AddAction takes two parameters - the SWFAction object to add, and a flag - when the action should execute. Options include SWFBUTTON_MOUSEUP as you see above, or alternatively SWFBUTTON_MOUSEDOWN, SWFBUTTON_MOUSEOVER, and more - see the documentation for a full list.
As the first parameter to addAction, we pass in "new SWFAction(...)" - the constructor of the SWFAction class takes a string that contains the ActionScript code you wish the action to execute. For this action, which will execute when the user clicks the mouse button on the object, we want to execute the GetUrl ActionScript function. In the example, GetUrl is passed two parameters - the URL to load, and the name of the window to load it in. If the named window does not exist, it will be created for you. I don't want to descend much further into the depth of ActionScripting, so I suggest you refer to the online ActionScript dictionary - http://www.macromedia.com/support/flash/action_scripts/
So, the addAction line translates to "Create a new ActionScript action that will load the Slashdot website into a new window, then attach that action to our button so that it executes whenever the user clicks the button".
After the action code, there is a slight change to the normal procedure - we use $movie->add() as before, except this time we grab the return value and store it in the $displayitem variable. This is done because, when adding shapes, text, buttons, and sprites to a movie, the add() function returns a special type of object, SWFDisplayItem(), which is a handle to the object inside the movie. This means you can add the same button (or shape, text, etc) to the movie several times over and manipulate them individually without much fuss.
This functionality is important here, because you cannot manipulate the position of an SWFButton object directly - you need to add it to the movie first, then manipulate the position of the returned SWFDisplayItem object, and you can see the line after the add() call we do just that.
Finally, the movie is sent to output as per usual. If you would like to make your button more interesting (I only have so much space here!), you might want to try combining the previous code regarding text with this button to make something more interesting.
Want to learn PHP 7?
Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!
If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!
Next chapter: Animation >>
Previous chapter: Flashy text
Jump to:
Home: Table of Contents
Copyright ©2015 Paul Hudson. Follow me: @twostraws.