We create a highscore MovieClip that functions independently. It must fulfill these functional requirements.
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
ActionScriptloadVariables ("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. ActionScriptif (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
|