Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Now we'll talk about events and event handlers. Events are things that happen. One example of an event is a mouse click. Another example is when a file finishes downloading or when a song finishes playing. So those are some examples of events and again they're just things that happen. An event handler is a function that responds to the event. For example when a person clicks on a button something happens, the button navigates to a certain frame. Event handler is what controls that functionality, or what block of code runs when a certain event happens.
So let's take a look at writing an event handler function. If you're following along the file I'm working in is called 03_Events.fla in the Chapter 02 folder in the Exercise Files folder. Select the first keyframe of the Actions layer and open the Actions panel using the keyboard shortcut Option+F9 on the Mac or F9 on the PC. In the Actions panel, define a function. Let's say this function is running when a person's mouse clicks a certain button. So I'm going to call this buttonClicked with capital C. That's the name of the function.
It's going to received a value. I'm going to give the name of the value of event. When an event handler function runs, it receives information about the event that happened. So we need to create a parameter to accept that event. And I'm calling the parameter event here. I'm going to type a colon to declare the data type of the event, and because the event happens with the mouse, it's classified under the data type MouseEvent And again it has a capital M. So MouseEvent, capital E. The returned data type for event handlers is almost always void.
And other than that an event handler looks pretty much just like a function. And there's a look in creating a function that reacts to an event. Now let's make this function do something very simple. So I'll type Trace. Remember a trace statement makes something appear in the output window. And then parentheses after trace, I'm going to type in a value in quotes. The value is going to be "button clicked". So whenever this event handler function runs, the message "button clicked" will appear in the Output window. Now, in order for this function to run we need to connect it to some sort of object and we'll talk about that in the next movie.
Get unlimited access to all courses for just $25/month.Become a member
58 Video lessons · 66255 Viewers
61 Video lessons · 96766 Viewers
56 Video lessons · 110467 Viewers
82 Video lessons · 105157 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.