Register two event listeners
The first four statements in Listing 10 would be sufficient to play the effect if that was all that I wanted to do. In addition, however, myobjective is to illustrate the servicing of events that are dispatched due to changes of state within the program.
Seven different types of events
A Resize object dispatches seven different types of events including the following two:
- effectStart - Dispatched when the effect starts playing.
- effectEnd - Dispatched when the effect finishes playing, either when the effect finishes playing or when the effect has been interrupted by a call to the end() method.
The last two statements in Listing 10 register event listeners on the Resize object for both of those types of events.
Listing 10 also signals the end of the constructor for the Driver class.
Event handlers registered on the Resize object
The two event handlers registered on the Resize object by Listing 10 are shown in Listing 11.
Event handlers registered on the resize object.
private function startEffectHandler(
event:EffectEvent):void{textOut.text += "\nEffect Started!";
} //end event handler//--------------------------------------------------//private function endEffectHandler(
event:EffectEvent):void{textOut.text += "\nEffect Ended!";
} //end event handler
The first method named startEffectHandler is executed each time the Resize object dispatches an effectStart event. The second method named endEffectHandler is executed each time the Resize object dispatches an effectEnd event.
Output text at the start and the end of the effect
The text in the text area at the top of Figure 5 shows the result of executing the startEffectHandler method from Listing 11.
The text in the text area at the top of Figure 6 shows the result of executing the endEffectHandler method from Listing 11.
(Note the small size of the image at the end of the resize effect inFigure 6 as compared to the size of the image at the beginning of the resize effect in Figure 4.)
Methods of the Resize class
The Resize class defines several methods including the following six (in alphabetical order) :
- end - Interrupts an effect that is currently playing, and jumps immediately to the end of the effect.
- pause - Pauses the effect until you call the resume method.
- play - Begins playing the effect.
- resume - Resumes the effect after it has been paused by a call to the pause method.
- reverse - Plays the effect in reverse (if the effect is currently playing) , starting from the current position of the effect.
- stop - Stops the effect, leaving the effect targets in their current state.
Common event handler for the buttons
Listing 12 shows a common event handler that is used to service click events on all six of the buttons at the bottom of Figure 4.
Common event handler for the buttons.
private function btnHandler(event:MouseEvent):void{
if (event.target == startButton) {resize.play();//start the effect
startButton.enabled = false;}else if(event.target == pauseButton){
resize.pause();//pause the effect}else if(event.target == resumeButton){
resize.resume();//resume the effect after a pause}else if(event.target == reverseButton){
resize.reverse();//reverse the effect}else if(event.target == endButton){
resize.end();//end the effect prematurely}else{//reset the program to starting conditions
resize.end();image.width=240;
image.height=240;startButton.enabled=true;
} //end else} //end btnHandler