• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 14. Extending ActionScript > Listeners and Watchers

Listeners and Watchers

The next couple of sections cover topics not directly related to callbacks and classes. However, both listeners and watchers fit here nicely because they provide things that are either difficult or impossible to achieve with callbacks or classes. Specifically, the fact that you can have only one callback function defined per event is effectively circumvented with listeners. Although they’re not the same as events, listeners are cumulative; therefore, you can have several listeners listening for the same event. Similarly, watchers get around a frustrating fact about getter/setter properties. Specifically, properties are not continuously updated. Only when a script says, “Hey, change this property” or “What’s that property’s value?” does Flash go and set or get that property. Although this way is more efficient, it’s frustrating because you might want to monitor one property’s value. If the value ever goes too high, for example, you might want to respond. It’s not totally efficient to be continuously checking a property’s value—but watchers do that in an efficient way. The truth be told, you can’t actually use watchers on getter/setter properties, and you can’t use listeners on most events; nonetheless, they’re useful and certainly worth studying as a comparison.

Listeners Basics

Listeners enable you to define which function will trigger when a particular event occurs. It’s almost the same as callback functions, although the list of events that you can use as listeners does not include every event imaginable. For example, no button events (such as onPress) can be used as a listener. The advantage of listeners is that they’re easy to add and remove and they don’t conflict with each other. That is, one event (say a text field instance’s onChanged event) can be listened for and trigger a particular function. You can easily add more listeners or other events without affecting existing listeners.


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint