Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Design > Adobe Creative Suite

Advanced Buttons and Event Handling in Adobe Flash CS5

  • Print
  • + Share This
This chapter is from the book

Creating graphics and animation in Flash is only half the story. The other half is interactivity, which involves giving the viewer control of those graphics and animation. What makes a movie interactive? It’s the back-and-forth communication between the user and the movie. Mouse movements, button clicks, or keypresses are examples of things that happen, called events. Events form the basis of interactivity. There are many kinds of events—some are user driven whereas others are not. You’ll learn to make Flash listen for and respond to these events (event handling).

This chapter first introduces events, listeners, and functions used to respond to events. Next, it explores the simplest class for creating interactivity: the SimpleButton class. You’ll learn about invisible buttons, animated buttons, and more complex buttons. You’ll also learn about the classes and events that are involved in keyboard input and the context menu. Additionally, you’ll learn an important event known as the ENTER_FRAME event, which you’ll rely on to create continuously running actions. Understanding these classes and event handling is essential to creating Flash interactivity because these elements are the scaffold on which you’ll hang virtually all your ActionScript.

Listening for Events


With all these events happening, you need a way to detect and respond to them. You detect an event by creating an event handler. An event handler simply tells Flash what to do when a specific kind of event happens. Creating an event handler is a two-part operation: first, you add, or “register,” a listener to detect the event and trigger a function, and second, you create the function that tells Flash how to respond. (It doesn’t matter if you register the listener first or create the function first. As long as they are both in the same block of code, the event handler will work.)


myButton_btn.addEventListener(MouseEvent.CLICK, reportClick);



function reportClick(myevent:MouseEvent):void {
   // do something in response




  • + Share This
  • 🔖 Save To Your Account