• 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 to 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 Actions > Miscellaneous Actions > setInterval.

3.
In the Parameters field, enter the name of a function, followed by a comma and then an interval (in milliseconds) (Figure 4.101).

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


The function will be called continuously at the specified interval.

4.
Choose Actions > User-Defined Functions > function (Esc + fn).

5.
In the Name field of the Parameters pane, enter the name of your function; leave the Parameters field blank (Figure 4.102).

Figure 4.102. Name your function in the Name field of the Parameters pane.


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.103).

Figure 4.103. 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 (Actions > 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 Actions > Miscellaneous Actions > setInterval.

3.
In the Parameters field, 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.104).

Figure 4.104. 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 object, 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 by using set variable or evaluate. 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.
Select the setInterval action, and delete it.

3.
Choose Actions > Variable > set variable.

4.
In the Variable field, enter a name to give your setInterval action (Figure 4.105).

Figure 4.105. The name of your setInterval action goes in the Variable field.


5.
In the Value field, create the same setInterval action you had before, and check the Expression checkbox (Figure 4.106).

Figure 4.106. The entire setInterval action goes in the Value field.


The name in the Variable field identifies the setInterval action in the Value field.

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

7.
In the Parameters field, enter the name identified with the setInterval action (Figure 4.107).

Figure 4.107. 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.


When Flash encounters the clearInterval action, it removes the setInterval and stops the continuous actions.

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