Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Design > Adobe Creative Suite

  • Print
  • + Share This
This chapter is from the book

Creating Continuous Actions

So far, you've learned ways to execute an action in response to events that happen when the user does something. But on many occasions, you'll want to perform an action continuously. An if statement, for example, often needs to be performed continuously to check whether conditions in the movie have changed. Another example is if you want to animate an object moving across the Stage purely with ActionScript. In that case, you'll want to perform a command that changes its position continuously.

The Event.ENTER_FRAME event happens continuously. The event is triggered at the frame rate of the movie, so if the frame rate is set to 12 frames per second, the ENTER_FRAME event is triggered 12 times per second (Figure 4.62). Even when the Timeline is stopped, the event continues to happen. This setup is an ideal way to make actions run on automatic pilot; they will run as soon as the event handler is established and stop only when the event handler is deleted or the object on which it is defined is removed.

Figure 4.62

Figure 4.62 The ENTER_FRAME event happens at the frame rate of the movie, which you can change in the Property inspector. Typical frame rates for online playback are between 12 and 15 frames per second.

To create continuous actions with the ENTER_FRAME event

  1. Select the first frame of the main Timeline, and open the Actions panel.
  2. In the Script pane, create a function with the Event object as its parameter:
    function movecar(myevent:Event):void {
    In this example, when the function is called, the movie clip called car_mc will move 5 pixels upward.
  3. On the next line, attach the addEventListener() method to the Stage or to an object:
    car_mc.addEventListener (Event.ENTER_FRAME, movecar);
    In this example, the listener is added to the movie clip object and will detect the ENTER_FRAME event, which happens continuously at the frame rate of the Flash movie (Figure 4.63).
    Figure 4.63

    Figure 4.63 Flash continuously moves the movie clip called car_mc 5 pixels up the Stage. An if statement has been added to check if the movie clip has moved beyond a certain point and, if so, removes the event listener.

  4. Create a movie clip symbol and place an instance of it on the Stage. In the Property inspector, name it car_mc to match the ActionScript code.
  5. Choose Control > Test Movie.

    At the frame rate of the Flash movie (12 times a second if the frame rate is 12 fps), the ENTER_FRAME event occurs and your function is called, moving the movie clip on the Stage upward continuously (Figure 4.64).

    Figure 4.64

    Figure 4.64 The movie clip named car_mc is put on the Stage.

Using Timers

The ENTER_FRAME 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 Timer class instead.

When you create an object from the Timer class, the new object dispatches a TimerEvent event at regular intervals. You specify how long those intervals are (in milliseconds) and how many intervals there will be. You can then add an event handler to listen and respond to each event.

The TimerEvent has two specific events: a TimerEvent.TIMER event that happens at each interval, and a TimerEvent.TIMER_COMPLETE, which happens at the end of the timer.

To create continuous actions with a timer

  1. Select the first frame of the main Timeline, and open the Actions panel.
  2. Instantiate a new Timer object. The constructor takes two parameters—the first is a number (in milliseconds) for the timer interval, and the second is the number of intervals. The second parameter is optional, and if left out, your timer will run forever until deleted.
    var myTimer:Timer=new Timer(10,1000);
    The function will be called every 10 milliseconds (1/100th of second). There will be 1,000 intervals, so this timer lasts 10 seconds.
  3. On the next line, call the start() method in order to begin the timer:
    The two lines of code so far create a Timer object and start it (Figure 4.65).
    Figure 4.65

    Figure 4.65 A new Timer object called myTimer is created and started.

  4. Next, add an event handler to detect the TimerEvent.TIMER events that are being dispatched every 10 milliseconds:
    function movecar (myevent:TimerEvent):void{
    myTimer.addEventListener (TimerEvent.TIMER, movecar);
    This listener detects the TimerEvent.TIMER event and calls the function called movecar, moving the movie clip upward continuously, until the timer stops.
  5. On the Stage, add a movie clip instance called car_mc. Test your movie by choosing Control > Test Movie (Figure 4.66).
    Figure 4.66

    Figure 4.66 At each 10-millisecond interval for 1,000 intervals, a TIMER event happens. Each time it happens, the movie clip called car_mc moves 5 pixels up the Stage, animating the graphic.

To detect the end of a timer

  • Add an event handler to detect the TimerEvent.TIMER_COMPLETE event. The following code is an example:

    myTimer.addEventListener (TimerEvent.TIMER_COMPLETE, stoptimer);
    function stoptimer (myevent:TimerEvent):void{
         // do something

    The function called stoptimer is called only after the timer called myTimer has completed all of its intervals.

  • + Share This
  • 🔖 Save To Your Account