• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 4. Advanced Buttons and Event De... > Creating Continuous Actions with set...

Creating Continuous Actions with setInterval

The enterFrame event, although easy to use and effective for creating most continuous actions, is limited to the frame rate of your Flash movie. If you want to perform an action on a continuous basis but do so at specific intervals, you should use the action setInterval instead.

You can use the setInterval action in two basic ways. In the first way, you provide two parameters. The first parameter is a function that is invoked on a continuous basis; the second parameter is the interval (in milliseconds) that separates the function invocation. So if you want the function called blinkingLight to be called every 5 seconds, you can write the statement setInterval (blinkingLight, 5000). Then you then must define the blinkingLight function.

The second way to use setInterval is to provide an object, its method, and an interval. Instead of calling a function at a periodic interval, Flash calls the method of the object at a periodic interval. This method can be one that already exists or one that you define yourself. You can call the nextFrame() method on the main Timeline every second with this statement:

setInterval (_root, "nextFrame", 1000)

Notice that the method name is in quotation marks.

To create continuous actions with setInterval (first way):

1.
Select the first frame of the main Timeline, and open the Actions panel.

2.
Choose Global Functions > Miscellaneous Functions > setInterval.

3.
In between the parentheses, enter the name of a function followed by a comma and then an interval (in milliseconds) (Figure 4.113).

Figure 4.113. The function called slideshow will be called every 3 seconds.


The function will be called continuously at the specified interval.

4.
On a new line, choose Statements > User-Defined Functions > function (Esc + fn).

5.
Directly to the right of the function that was created in the preceding step, enter a name for the function (Figure 4.114).

Figure 4.114. A new function named slideshow() has been created.


This name is the same one that you used in the setInterval action.

6.
Choose actions for your function that you want to execute at the periodic interval (Figure 4.115).

Figure 4.115. This movie contains images from your recent trip to the zoo. The images are in separate keyframes. The setInterval action advances to the next image every 3 seconds automatically. The stop action has been added to prevent the images from playing until setInterval calls the slideshow function.


✓ Tip

  • Add the action updateAfterEvent (Global Functions > Movie Clip Control > updateAfterEvent) to your function if you are modifying graphics at a smaller interval than your movie frame rate. This method forces Flash to refresh the display, providing smoother results.


To create continuous actions with setInterval (second way):

1.
Select the first frame of the main Timeline, and open the Actions panel.

2.
Choose Global Functions > Miscellaneous Actions > setInterval.

3.
In between the parentheses for setInterval(), enter the name of an object followed by the name of its method in quotation marks and then an interval (in milliseconds), separating your parameters with commas (Figure 4.116).

Figure 4.116. This setInterval does the same job as in the preceding example, except that it advances every second.


The method will be called continuously at the specified interval. This example uses a method of the Movie Clip class, but you can define and use your own method. See Chapter 11 for more information about creating your own methods and objects.

✓ Tips

  • In both ways of using the setInterval action, you can pass parameters. In the first way, you can pass parameters to your function, and in the second way, you can pass parameters to your method. If the method you define in setInterval requires parameters, you must provide them at the end of the parameters for setInterval, as in this example: setInterval (myObject, "myMethod", 1000, parameter1, parameter2) This setInterval will call myObject.myMethod(parameter1, parameter2) every second.

  • Although setInterval uses milliseconds to determine when to trigger a function or a method, it must do so during frame transitions set by the movie frame rate. This setup makes the interplay between setInterval and the frame rate a little tricky and makes exact timing less direct than you’d expect. At 10 frames per second, for example, a frame transition occurs every 100 milliseconds. If you define an interval of 200 milliseconds, Flash waits two frames (200 ms) until the interval passes; then it triggers the method or function on Frame 3. It waits two more frames (another 200 ms) and then triggers on Frame 6. Finally, after two more frames, it triggers on Frame 9.

    If you change your movie to play at 20 frames per second, a frame transition occurs every 50 milliseconds. Flash waits four frames (200 ms) and triggers the method or function on Frame 5. It waits another four frames and triggers on Frame 10. It continues to trigger on every fifth frame—at Frame 15 and Frame 20. If you compare the two movies, the one at 10 fps gets triggered three times in the first second (Frames 3, 6, and 9), whereas the one at 20 fps gets triggered four times in the first second (Frames 5, 10, 15, and 20). So even though they both have the same 200-ms interval defined in setInterval, frame rates can have significant effects.


The setInterval action continues to call the function or method at regular intervals until the movie ends or until you remove the setInterval action with clearInterval. The action clearInterval requires one parameter, which is the name you give the setInterval action. In the preceding examples, you didn’t name the setInterval action because you had no intention of stopping it. In this next task, you will name your setInterval action. Then, you can clear it by using the clearInterval action.

To clear a setInterval action:

1.
Continuing with the preceding example, select the first frame of the main Timeline, and open the Actions panel.

2.
Place your pointer before the setInterval action, and insert a variable name and an equals sign (Figure 4.117).

Figure 4.117. A variable name has now been assigned to the setInterval so that you can identify and clear the interval.


3.
At a later point in your Flash movie, when you want to stop the setInterval, choose Global Functions > Miscellaneous Functions > clearInterval.

When Flash encounters the clearInterval action, it removes the setInterval and stops the continuous actions (Figure 4.118).

Figure 4.118. When the clearInterval action is assigned to a button (top), it will remove the setInterval identified as myInterval. The movie will no longer advance to the next frame continuously, and the slide show will stop.


  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint