- Listening for Events
- Mouse Detection
- The SimpleButton Class
- Invisible Buttons
- Animated Buttons and the Movie Clip Symbol
- Complex Buttons
- Button-tracking Options
- Changing Button Behavior
- Creating Buttons Dynamically
- Keyboard Detection
- The Contextual Menu
- Creating Continuous Actions
- A Summary of Events
Creating Continuous Actions
So far, you’ve learned ways to execute an action in response to events that happen when the user does something—whether a mouse click or a keyboard press. 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 removed or the object on which it is defined is removed.
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 24 frames per second.
To create continuous actions with the ENTER_FRAME event:
- Select the first frame of the main Timeline, and open the Actions panel.
In the Script pane, assign 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.
On the next line, create a function with the Event object as its parameter:
function movecar(myevent:Event):void { car_mc.y-=5; };
In this example, when the function is called, the movie clip called car_mc will move 5 pixels upward (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.
- 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.
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 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 restricted to the frame rate of your Flash movie. If you want to perform an action on a continuous basis and do so at an interval that you specify, 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:
- Select the first frame of the main Timeline, and open the Actions panel.
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 stopped.
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.
On the next line, call the start() method to begin the timer:
myTimer.start();
The two lines of code so far create a Timer object and start it (Figure 4.65).
Figure 4.65 A new Timer object called myTimer is created and started.
Next, add an event handler to detect the TimerEvent.TIMER events that are being dispatched every 10 milliseconds:
myTimer.addEventListener (TimerEvent.TIMER, movecar); function movecar (myevent:TimerEvent):void{ car_mc.y-=5; };
This listener detects the TimerEvent.TIMER event and calls the function called movecar, moving the movie clip upward continuously until the timer stops.
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 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.