Adobe Flash Professional CS6 Adobe Flash Professional CS6 Classroom In A Book | Page 232

The first step in event handling is to create a listener that will detect the event. A listener looks something like this: wheretolisten.addEventListener(whatevent, responsetoevent); The actual command is addEventListener() . The other words are placeholders for objects and parameters for your situation. Wheretolisten is the object where the event occurs (often a button), whatevent is the specific kind of event (such as a mouse click), and responsetoevent is the name of a function that is triggered when the event happens. So if you want to listen for a mouse click over a button called btn1_btn , and the response is to trigger a function called showimage1 , the code would look like this: btn1_btn.addEventListener(MouseEvent.CLICK, showimage1); The next step is to create the function that will respond to the event—in this case, the function called showimage1 . A function simply groups a bunch of actions together; you can then trigger that function by referencing its name. A function looks something like this: function showimage1 (myEvent:MouseEvent){ }; Function names, like button names, are arbitrary. You can name functions what- ever makes sense to you. In this particular example, the name of the function is showimage1 . It receives one parameter (within the parentheses) called myEvent , which is an event that involves the mouse. The item following the colon indicates what type of object it is. If this function is triggered, all the actions between the curly brackets are executed. adding the event listener and function You’ll add ActionScript code to listen for a mouse click on each button. The response will make Flash go to a particular frame on the Timeline to show different content. 1 Select the first frame of the actions layer. 2 Open the Actions panel. 3 In the Script pane of the Actions panel, beginning on the second line, type gabelloffel_btn.addEventListener(MouseEvent.CLICK, restaurant1); The listener listens for a mouse click over the gabelloffel_btn object on the Stage. If the event happens, the function called restaurant1 is triggered. Adobe FLAsh ProFessIonAL Cs6 CLAssroom In A book 223