Using Conditional Actions

I l @ ve RuBoard

Conditional actions allow you to ask certain questions (or test whether conditions have been met) and make Flash carry out another action if the answer is true (or the conditions have been met). You could ask Flash to determine, for example, whether the number that is currently in the variable ageVAR is greater than 59. If it is, you could instruct Flash to jump to a frame that describes discounts for senior citizens .

The basic conditional action is if . You use if to test the validity of a given statement. You might, for example, test whether the last frame of your movie loaded. You can use that information in conjunction with a looping teaser animation to tell your viewers that there will be a wait to download information.

To test for download of a frame:

  1. Create a 100-frame Flash file, and make every tenth frame a keyframe containing identifying text.

  2. In the first 20 frames , create a simple animation that will play while the user waits for the entire file to download.

    You might create a 20-frame animation of a ball moving around a rectangle containing the message "One moment while I get that for you "

  3. In frames 30 through 100, add a couple of bitmapped graphics, plenty of sounds, and/or video clips to ensure that the file is large.

    The larger the file, the longer it takes to download and the more time you'll have to check your preload message.

  4. In the Timeline, add a new layer to contain your actions, and label it Actions.

  5. In the Actions layer, select Keyframe 1.

  6. In the Actions-Frame panel, in the Actions Toolbox, Actions > Conditions/ Loops category, double-click if (Figure 15.16).

    Figure 15.16. To create an ActionScript that checks when a frame is loaded, from the Actions Toolbox or the Add menu in the Actions panel, choose Actions >Conditions/ Loops > if .

    graphics/15fig16.gif

    Flash updates the Script pane with the following code:

     if (<not set yet>){ } 
  7. In the parameters area, in the Conditions field, enter _framesloaded >= 100 .

    Flash updates Line 1 of the Script with the condition you enter (Figure 15.17). Here, the condition is "Has frame 100 loaded?"

    Figure 15.17. In this conditional action, the greater-than-or-equal-to symbol (referred to in ActionScript as a comparison operator) tells Flash to check whether the item on its left side is greater than or equal to the item on its right side. In this case, is the number of the frame most recently loaded 100 or greater?

    graphics/15fig17.gif

  8. With Line 1 selected in the Script pane, from the Ad menu, choose Actions > Movie Control > goto .

    Flash adds gotoAndPlay(1); to Line 2 in the script pane and displays the goto action's parameters.

    When Do You Need a Preloader?

    If your site contains large or complex animations, you might want to download much or all of the data to the user's computer before allowing the user to explore the site. Waiting until all the data is available locally ensures the smoothest possible playback. To keep users amused (and informed) during the download process, you might create a teaser animation with a small number of frames and fast-loading content (often referred to as a preloader ) that loops until the last frame of your animation has downloaded to the user's computer.

    By combining if with the _framesloaded property, you can check the user's computer to see whether the data for a certain frame already exists on that computer. If the data is there, Flash follows whatever instruction you include in the conditional test. If the data isn't there, Flash plays the next frame of the movie (or completes the next action, if there is one).

  9. In the parameters area, set the following parameters:

    • In the Scene field, enter <current scene> .

    • From the Type pop-up menu, choose Frame Number.

    • In the Frame field, enter 21 .

      Flash updates the Script pane to reflect the parameters that you chose (Figure 15.18).

      Figure 15.18. Flash carries out any instructions that fall between the curly braces. Adding gotoAndPlay (21); instructs Flash to jump to Frame 21 if the test for the presence of Frame 100 proves true.

      graphics/15fig18.gif

      In the published movie, Flash checks for Frame 100 before starting to play the movie. If Frame 100 is present on the viewer's computer, Flash jumps to Frame 21 and starts playing from there. If Frame 100 is absent, Flash moves on to Frame 2 of the movie and continues playing the movie.

graphics/01icon02.gif Tip

  • You don't have to specify an exact frame number in your conditional action; you can use the _totalframes property to make Flash check whether all the frames in the movie have loaded. In step 7 of the preceding exercise, in the Conditions field, enter _framesloaded >= _totalframes . If you change the length of your movie, you won't have to remember to go back and change your script.


To make the movie repeat the teaser (the first 20 frames of the movie) until Frame 100 arrives, at the end of the teaser animation, you need to create a loopthat is, tell Flash to start over at Frame 1.

To loop the teaser animation:

  1. Using the file that you created in the preceding exercise, in the Actions layer of the Timeline, insert a keyframe at Frame 20 (the last frame of the teaser animation) and select it.

  2. In the Actions-Frame panel, from the Add menu, choose Actions > Movie Control > goto .

    Flash adds gotoAndPlay (1); to the Script pane. The default parameters for the goto action are to go to the first frame of the current scene and start playing (Figure 15.19).

    Figure 15.19. Adding gotoAndPlay (1); to the last frame of your teaser animation tells Flash to jump to Frame 1 and start over. This action creates a loop that repeatedly tests to see whether Frame 100 has arrived. If not, Flash plays the teaser again.

    graphics/15fig19.gif

    When the playhead reaches Frame 20, Flash jumps back to Frame 1. There, the frame actions that you set up in the preceding exercise test for the presence of Frame 100 all over again.

The Pitfalls of Conditional Actions

In the following ActionScript, Flash carries out the action between the opening and closing curly braces only when the test result is true :

 if (_framesloaded >= 100) {         stopAllSounds;  }  gotoAndPlay (21) 

Unless that action within the curly braces takes you to another frame, thereby canceling the script, Flash carries out any actions that come after the ending brace , whether the result is true or false . Here, the script would take you to frame 21 even if Frame 100 hadn't loaded yet, thus defeating the purpose of the conditional test. Make sure that all the actions that should take place only when the test result is true are placed inside the curly braces.

If you want Flash to carry out one action when the condition is true and a different action when the condition is false , you need to create a branching condition . Use if in combination with else or else if . These actions live in the Actions > Conditions/Loops category of the Actions Toolbox.

Conditionals for Error Checking

In the tasks in "Creating Scriptable Text Objects" earlier in this chapter, you restricted the length and selectability of a text box to help ensure that users enter data correctly. As you get more comfortable with scripting, you can use conditional actions ( if , else , if else ) to test for common user errors.

The following script is similar to the one you created, but it adds an if action to see whether someone accidentally enters letters where there should be numbers . The predefined function isNaN returns a value of true when the tested item contains items that are not numbers. This script catches cases in which users enter letters and displays a message in the text box, reminding them to use numbers:

 on (release) {    if (isNAN (ageVAR)) {       luckyNumVAR = "Use numerals only";     } else {       luckyNumVAR = Number (ageVAR) + 2;     }  } 

You can't view the workings of conditional actions in the movie-editing environment; you must view them in Test mode.

To preview conditional actions:

  1. Continuing with the file that you created in the preceding exercise, from the Control menu, choose Test Movie.

    Flash exports the movie and opens it in Flash Player.

  2. From Flash Player's View menu, choose Show Streaming (Figure 15.20).

    Figure 15.20. When you are in Flash Player and choose Control >Test Movie, menus offer several commands other than the ones that are available in movie-editing mode. Choose View >Show Streaming to see how your conditional action and preloader animation will work together as your movie streams over the Internet.

    graphics/15fig20.gif

    Flash simulates the way that your movie will stream over the Internet. (For more details on simulated streaming, see Chapter 16.) As a result, Frame 100 will not be available for a while, and you'll have a chance to see your teaser animation repeat.

graphics/01icon02.gif Tips

  • Creating a practice file for testing the _framesloaded property can be tricky unless you have a movie that you know will take several seconds to load. If your downloading message doesn't appear or appears too briefly for you to check it, add more sounds, video clips, or bitmaps to frames 30 through 100 of your movie to increase the file size and download time. In a real-world situation, if you have that "problem," you may not need a downloading message at all.

  • To ensure that your viewers get to the heart of your movie as soon as possible after the last frame loads, put the conditional test in every frame of the teaser animation.


I l @ ve RuBoard


Macromedia Flash MX for Windows and Macintosh. Visual QuickStart Guide
Macromedia Flash MX 2004 for Windows and Macintosh (Visual QuickStart Guides)
ISBN: 0582851165
EAN: 2147483647
Year: 2002
Pages: 243

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net