- 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 it’s 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 continuously change a property of an object to create an animation.
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 24 frames per second, the ENTER_FRAME event is triggered 24 times per second . 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.
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 .
- Create a movie clip symbol and place an instance of it on the Stage. In the Properties inspector, name it car_mc to match the ActionScript code.
- Choose Control > Test Movie > in Flash Professional.
At the frame rate of the Flash movie (24 times a second if the frame rate is 24 fps), the ENTER_FRAME event occurs and your function is called, moving the movie clip on the Stage upward continuously .
Using timers
The ENTER_FRAME event, although easy to use and effective for creating 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 .
- 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 > in Flash Professional .
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.