Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
As I explained in the last movie, listeners are like a radio. They listen for certain things to happen. So if I want something to respond to a mouse click then I need to have it listen for a mouse click. In this movie, what we're going to do is add an event listener to our boarder. It has the instance name of boarder_mc. I going to go up to the actions frame. And by the way, if you're following along, I'm in Listeners.fla which can be found in the chapter 04 exercise folder.
Let's select the first keyframe of the actions layer and open up the Actions panel. In order to add an event listener, I need to type first the object that's going to be listening for the event. Now if I want to click on the boarder and have him do something, then he's going to be listening for the event. So I'm going to type boarder_mc and then a dot. And now what we're going to do here is basically attach an event to the boarder. Now I'm going to type add all lowercase, capital E.
That'll be for addEventListener and L is capitalized as well. If it's highlighted in the code entry area, you can just press Enter on your keyboard and that will write out addEventListener and create an open parentheses for you. If you look in the code hinting area, Flash will tell you what it expects you to type in. So now the first going to type in when we have an addEventListener and open parentheses is the type of event that's being listened for. A mouse click is called an mouse event so that's the general type of event. So I'm going to type Mouse with a capital M then Event with a capital E, no spaces there.
It should turn blue. And then type a dot, and then you should get code hinting that pops up. And now we're telling Flash what mouse event we're listening for. So I'm going to type a capital C and then click should be highlighted and I can just press Enter or Return on my keyboard. So this is how we say we're looking for a mouse click MouseEvent is the event category and then when I type a dot, then it's a specific event inside of that category. And those specific events have capital letters, all caps. So our boarder is listening for a CLICK and then type a comma and then a space and then after that what we're going to do is specify a function that's going to run when you click on the boarder.
I'm going to call my function on, both lowercase and then Click, with a capital C. Then I'm going to type a close parentheses and a semicolon to end the statement. And so that is how you add an event listener to an object. You type the object name.addEventListener, then you specify the event type and then a dot, the name of the event, it's all uppercase letters, then a comma, then the function that runs when the event happens. In the next movie we'll write the function that will execute when you click on the boarder.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101734 Viewers
61 Video lessons · 88492 Viewers
71 Video lessons · 72312 Viewers
56 Video lessons · 104028 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.