Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
If we take the function example from the prior video and add one more step we arrive at the event listener. Event listeners are a way of handling and reacting to events, like mouse clicks and other things similar to that, and then calling functions when those events occur. Event listeners are actually a lot easier to understand then most people think. Imagine if you're back in grade school, and there is a classroom of ten people. When the bell rang at the end of the class, all ten students who were trained to understand what that meant, would get up and leave. Also, imagine if each of the ten of you, when walking through the door was told something by the teacher, and one of you was told to stand up when a certain word was pronounced.
In other words, one out of the ten students was trained to react to a single message, a single word being pronounced that would cause that student to stand up. During the class, when the teacher pronounced the word, one student out of the ten would stand. This demonstrates the power and effectiveness of event listeners. You have two situations here. One in which all ten students will react, which is the sounding of the bell that marks the end of class when all students would leave and another where a single student was told to listen for a particular message, and then a single student stood when a word was pronounced. All we have to do is set up the definition of the behavior, which is the function, that would be leaving the class, or standing up. Then we would define the event that triggered the action, so in our example, that would be the bell ringing, or the teacher saying the word, and then we simply associate the two. So, let's take a look at the structure of this file. Here on the stage in the upper right hand corner is a button. The instance name of this button is rect_btn, for rectangular button. Here in the listener, we see that instance name, and the add event listener being added to the button, which of course, I'll explain in just a moment. Now, let's look at the code. We have a function structure, which is exactly the same as the prior example, with one simple exception. We now have an argument to discuss. We skipped over that last time because our parenthesis were empty. So, let's review real quickly. We have the function keyword, defining the function, we have a unique name for this function, we have parenthesis, again let's skip the argument for just a moment. We have data type for return, we're not returning anything, so our data type is void, and then we have open and close brace with the instruction therein.
Now, we move on to the event listener. We already have the behavior we wish to trigger in the form of our function, now all we have to do is identify the event, and figure out how it calls that function. Over here on the right we have MouseEvent.CLICK. This is simply saying that the type of event we're working with is the mouse event, rather than event which would be for frame scripts, and we need to know which event associated with the mouse we're going to use and in this case, we're going to use the mouse click. Now we know what event we're seeking, we know what event we're trained to listen for, that is the click of the mouse. Now what do we do when we get that event? When we capture that event, when we know it occurred, what do we do? That is, we call the function by this name, onRectBtnClick. That is the function that we have defined previously. Now that we know the event that we are listening for, and we know what to do when we hear it, all we have to do is associate that pair to something that is trained to do the listening, and that is our button. So again, we define a function that is supposed to be triggered when the event occurs, we determine which event we're going to listen for, and what to do when we hear it, and then we simply say, who is supposed to be doing the listening? In this case, the button will listen for all of these things. So, when the button hears the mouse click, it will call for the function. Now, let's quickly talk about our argument. In simple terms, an argument is a kind of variable that only has life inside the function. It's purpose is to receive information from the function call, so the function can use that information to vary it's outcome. When determining argument names, such as this evt, short for event, we need to follow the same rules that we observed when naming variables. In this case, evt, or event will contain information about the event that triggered the function, so we also want to data type our argument and in this case, we're going to give it a data type of MouseEvent. And as you can see, that's what we're passing in, MouseEvent.CLICK. In practice, we'll be creating very simple event listeners in this course and therefore, we won't need to rely on this argument. However, in action script three essential training, we'll see that we can get information from the event such as which button was clicked on, or other related information. For now, we only need to know that the event must be there, and that as we discussed in the variable basics video, the data type should match the expected data to avoid errors and again, we're saying that we're going to pass in a mouse event and we are, MouseEvent.CLICK. So, let's try this ourselves, and let's tie in everything we've learned so far.
First, let's use a frame script to initialize our event listener. We'll choose the actions layer, frame 1, and we'll open up our Actions panel. Second, let's comment our code to make sure we know what we're doing later on. Let's put in a single line comment, or a double slash and we'll say we're "making navigation buttons". Next, let's create a variable that we can manipulate. So, we'll say we're declaring the variable with the "var" keyword, we'll give it a valid name, "nextSection," and we'll give it a data type of "String," and we'll give it a value of "home". Next, we'll set up a function to trigger when the event occurs.
So, let's say "function onHomeClicked," again an arbitrary name, let's call it onHomeClick, and then we'll say "(evt:MouseEvent)," and we're not going to return anything from the function, so we'll give it a data type of ":void," and we'll open up our braces here. Then, inside the braces let's just redefine the variable, "nextSection = "products:", so we can see that our function is working. In order to get some immediate results, let's use our "trace" action, and trace the value of that variable, so we can see the change. Finally, let's add our listener, and we called the button on stage "rect_btn," so we'll add to that, "_addEventListener," and we'll say "(MouseEvent.CLICK," that's the event we're looking for, and the function we want to call is "onHomeClick)". Great, so now we're all done, and let's check and make sure it works. Before we do that, we need to go through our script here just so we know what to expect. The first thing that occurs in this script, is that the variable is defined and populated. The second, is that the function and listener are defined. Here's the function in 5 through 8, and then in line 10, we've defined our listener. The contents of the function won't execute until they're called. The listener has been added to the button, which is set up to listen for our MouseEvent.CLICK. So, we start of with a value of home, when we then click on the button, the function is called, which re-evaluates or re-assigns a value of products to nextSection, and then nextSection is traced to the output window. So, the ultimate result is we get nothing initially in the output window, and when we click the button, the variable is re-defined and we get the new result in the output window. Alright let's test is to see how it works.
Control > Test Movie. So, we get initially nothing as we expected, let me hide this for you, when we click on our button, the output window shows us products. That's exactly what we wanted. Let's close our output window, and let's close our SWF and go back to our main movie. That's an overview of how to create an event listener, and now that we have this under our belt, we can take the same process and apply it to several buttons. We'll create multiple listeners and functions for our navigation buttons and then we'll begin to control other elements such as our components, and so on as we go through the course.
Get unlimited access to all courses for just $25/month.Become a member
58 Video lessons · 66403 Viewers
61 Video lessons · 96859 Viewers
56 Video lessons · 110556 Viewers
82 Video lessons · 105176 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.