Приглашаем посетить
Высоцкий (vysotskiy-lit.ru)

Section 18.3.  Actions

Previous
Table of Contents
Next

18.3. Actions

Through its powerful ActionScript language, Flash provides a 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 long) examples on how to make use of various functions.

This next script gives you a quick start in using ActionScript:

    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( );

That script uses a custom function, MakeActionBox( ), to handle some of the grunt work you will experience when working with the SWFButton class. The SWFButton class, used for the $button variable, has several "states" that each require a shapehow 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 requires a complete shape of its own, so their creation is automated by using the function MakeActionBox( ).

Going through the main chunk of code line by line, you can see it creates an instance of SWFButton and stores it in the $button variable. Four functions are then called: setUp( ), setOver( ), setDown( ), and setHit( ). These define how this button should look when the user interacts with it. The example is quite short; you will find it is more visually appealing to have more than just the color change between states!

Next we come to the important function of this particular script: addAction( ). This takes two parameters: the SWFAction object to add and a flag for when the action should execute. Options include SWFBUTTON_MOUSEUP as seen above or, alternatively, SWFBUTTON_MOUSEDOWN, SWFBUTTON_MOUSEOVER, and moresee 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. So, the addAction( ) line translates to "Create a new ActionScript action that will load the Slashdot web site into a new window, then attach that action to our button so that it executes whenever the user clicks the button."

After the ActionScript code, there is a slight change to the normal procedurewe 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 objectSWFDisplayItem( )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 because you cannot manipulate the position of an SWFButton object directlyyou need to add it to the movie first, then manipulate the position of the returned SWFDisplayItem object. In the line after the add( ) call, we do just that.

Finally, the movie is sent to output as usual. If you would like to make your button more interesting, you might want to try combining the previous code for manipulating text. To make your ActionScript more interesting, try reading the ActionScript documentation, available from http://www.macromedia.com/support/flash/action_scripts/.


Previous
Table of Contents
Next