Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Linux Timesaving Techniques For Dummies.pdf
Скачиваний:
59
Добавлен:
15.03.2015
Размер:
15.98 Mб
Скачать

Building Interactive Movies with Ming

411

You can watch the movie with any Flash player. If you have a Flash plug-in installed in your Web browser, simply open the colors.swf file in the browser, and the movie appears. You’ll see most of the blue fade out of the photo that you chose and then slowly fade back in. With the right photo, the effect is stunning.

Building Interactive

Movies with Ming

You can also build interactive Flash movies with Ming. When you view an interactive movie, you can manipulate on-screen objects with the mouse and keyboard. Listing 53-3 shows a short Ming program that produces a movie that contains ActionScript. The movie draws a small blue circle that you can drag around with the mouse.

Lines 4–18: Defining the script

Lines 4–18 define a string of Flash ActionScript. In this case, you’re defining three anonymous functions related to an object named circle (you create circle in just a moment):

The first function is fired when you click the mouse while the cursor is over the circle. It simply calls the circle’s startDrag() method.

The second and third functions fire when you release the mouse button (after you’ve started a drag operation). Both functions call the circle’s stopDrag() method.

You can include almost any ActionScript verb or function that Macromedia defines (see the Ming Web site ming.sourceforge.net for restrictions).

Line 20: Declaring local variables

Examining the program

The following sections take a closer look at the dragme program.

Now look through the main() function. Unlike the first Ming program in this technique, dragme produces a movie that can be viewed only by a Flash player newer than version 5 (ActionScript is a recent addition to Flash).

LISTING 53-3: DRAGME.C

1 #include <ming.h>

2 #include <stdlib.h>

3

4 const char * action_script =

5“circle.onPress = function()”

6“{“

7

this.startDrag(‘’);”

8

“};”

 

9

 

 

10“circle.onRelease = function()”

11“{“

12

stopDrag();”

13

“};”

 

14

 

 

15“circle.onReleaseOutside = function()”

16“{“

17

stopDrag();”

18

“};”;

 

(continued)

412 Technique 53: Using Open-Source APIs to Save Time

LISTING 53-3 (continued)

19

 

 

20

int main( int argc, char * argv[] )

21

{

 

22

SWFMovie

movie;

23

SWFShape

circle;

24

SWFFill

fill;

25SWFMovieClip sprite;

26SWFDisplayItem f1;

27

28 Ming_useSWFVersion( 6 );

29

30movie = newSWFMovie();

31SWFMovie_setDimension( movie, 550, 400 );

32SWFMovie_setBackground(movie, 200, 200, 200 );

34circle = newSWFShape();

35

fill

= SWFShape_addSolidFill( circle, 0, 0, 128, 255 );

36SWFShape_setRightFill( circle, fill );

37SWFShape_drawCircle( circle, 40 );

38

39sprite = newSWFMovieClip();

40SWFMovieClip_add( sprite, (SWFBlock)circle );

41SWFMovieClip_nextFrame( sprite );

42

43 f1 = SWFMovie_add( movie, (SWFBlock)sprite );

44SWFDisplayItem_setName( f1, “circle” );

45SWFDisplayItem_moveTo( f1, 100, 100 );

47SWFMovie_add( movie, (SWFBlock)compileSWFActionCode( action_script ));

48SWFMovie_save( movie, “dragme.swf”, -1 );

50exit( EXIT_SUCCESS );

51}

Line 28 and 30–35: Creating the new movie

Use the Ming_useSWFVersion() function to tell Ming that you want to create a Flash version 6 movie (see line 28).

At lines 30–33, dragme creates a new movie, defines the width and height of the movie, and sets the background color to a light gray.

Next, dragme creates a circle of radius 40 (line 34) and fills it with an opaque shade of blue (line 35).

Lines 39–45: Adding movement

At line 39, you create a sprite — a sprite is something that can move around on the screen. Line 40 adds the shape (the circle created at line 34) to the sprite with SWFMovieClip_add(). Line 41 creates a new frame for the sprite. Line 43 adds the sprite to the movie by calling SWFMovie_add(), which returns a handle to the sprite display item.

Look back at the ActionScript that starts at line 4 — each function refers to an object named circle. Line

Building Interactive Movies with Ming

413

44 assigns a name (circle) to the display item added at line 43; after you’ve assigned a name to an object, you can manipulate that object in ActionScript code.

Lines 47–48: Compiling and saving the code

Line 47 compiles the action_script code (defined starting at line 4) into a more compact (and efficient) form and adds the result to the movie. Finally, the movie is saved to a file named dragme.swf (see line 48).

Compiling the program

To compile the dragme program, follow these steps:

1. If you’ve already created a Makefile (as we described earlier in the section titled “Compiling the program”), open the Makefile in your favorite editor, and add the following line to the end:

dragme: dragme.c

If you’re creating a Makefile from scratch, the result should look like this:

LDFLAGS += -lming -lz

LDFLAGS += -lungif -lpng12 -lm

colors: colors.c dragme: dragme.c

2. Save your work and close the editor.

3. Type make and press Enter.

The compiler starts running.

If you see any error messages, correct the problems before proceeding.

Running the program

After you’ve compiled the program, run ./dragme to produce a movie file named dragme.swf. You can open the dragme.swf movie in any Flash player that supports Flash version 6.0 or later. When the movie

appears, click the blue circle and drag it around with your mouse.

You can find more information about the Flash movie format on the Web (just do a Google search for SWF). You can also find a number of books that cover Flash and ActionScript — we recommend Flash ActionScript For Dummies by Doug Sahlin (published by Wiley).

54 Timesaving

PHP Tricks

Technique

Save Time By

Using curl with PHP to retrieve data from the Internet

Using XML data in PHP programs

Using the XML parser to manage XML data

Generating e-mail with PHP when something goes wrong

When you write a program in PHP, chances are the program will run within a Web server (most often, Apache). Although PHP is a powerful, general-purpose programming language, most

people use PHP to generate dynamic HTML Web pages.

In this technique, we show you how to leverage the power of PHP interfaces to reach from one Web server to another. Using PHP’s curl interface, your PHP programs can retrieve information from remote servers (Web servers, FTP servers, and so on). After you have the information you need, you can reformat it, pick out only the pieces that you’re interested in, and combine it with information from other sources for display on your own Web site.

We also show you how to use PHP’s XML parser to find your way through an XML data stream. If you haven’t run into XML before, think of it as self-identifying data. For example, the different pieces of a weather report (dew point, wind speed, and humidity) might be represented by the following XML snippet:

...

<DewPoint>72°F</DewPoint>

<Wind>From the West at 23 mph</Wind>

<Humidity>98%</Humidity>

...

You can see that each element is surrounded by a pair of tags that describes the data within. XML data can get pretty complex, but PHP’s XML parser makes it easy to deal with.

We show you how to combine PHP, curl, and XML to quickly incorporate weather reports (or just about any other type of dynamic data) in your own Web pages. You also find out how to save a ton of development and debugging time by making your PHP programs send you an e-mail when something goes wrong. Imagine a program that notifies you whenever it runs into something it’s not prepared to deal with. That’s a whole lot easier (and faster) than wading through debug logs. You’ll never have

to ask your users for a complete error message again.

Соседние файлы в предмете Операционные системы