Flash and XML[c] A Developer[ap]s Guide

We create a highscore MovieClip that functions independently. It must fulfill these functional requirements.

  1. When it starts, it begins to load the data.

  2. While loading, it displays animation.

  3. It continues to test for a successful load.

  4. It displays the data if successful.

  5. It displays a special frame if the download fails.

We can accomplish all this with four frames. The only important graphic element is a text element in frames 2 and 3 that is linked to a local variable called score. It is backed by a simple colored rectangle and title (Figure 7.3). Frame 1 starts the load with

Figure 7.3. highscore Display

ActionScript

loadVariables ("highscore.txt", this);

Frame 2 is labeled LoopIfNotLoaded. It has no code and serves only for loopback. Frame 4 is labeled LoadingError. It has any appropriate graphic or action. In this case, the highscore is a completely optional part of the game. There's no reason to disturb the game if it cannot be found, so the LoadingError state displays nothing at all.

All the rest of the functionality is in frame 3. If the load is successful, it will create a variable at this scope called highscore. When it discovers this variable, the code copies it to the score variable to be displayed and stops.

ActionScript

if (this.highscore) { score=highscore; stop(); } else{ if( (score=tries++) <90){ gotoAndPlay ("LoopIfNotLoaded"); } else{ gotoAndStop ("LoadingError"); } }

Until the highscore variable appears, tries is incremented, which counts the number of unsuccessful loops . Simply to keep the screen lively, this count is copied to score for display. This display is chosen because it is easy to create and gives the appearance of progress. Since score uses a unique font, we take care to put only digits into this display. If we use only digits, we can include a much smaller font in the swf file.

The count is tested every frame. If the number of retries is not unreasonable, it loops back for more. Otherwise, the ActionScript gives up and ends gracefully in the LoadingError state.

NOTE

M OVIE E XPLORER

The script in a Flash movie is typically distributed across multiple frames. This allows for beautiful integration of graphics and software. But it is often difficult to see what is going on. We end up clicking from frame to frame, and we open and reopen ActionScript windows .

Flash provides a tool to organize the scramble of code into a fairly linear form. The Movie Explorer is a flexible and powerful viewer that allows you to view the structure of your application at whatever level of detail you wish and filtered usefully.

Our example can be exposed all at once in the Movie Explorer. We can follow the program flow within a frame and from frame to frame.

Категории