Event handlers are the most common way to work with events in Flash. Event handlers are nothing more than methods on objects. For example, the XML class has an event handler named onLoad. This onLoad event handler is actually a method of the XML class. When we create an instance of the XML class we can handle the onLoad event by overriding the onLoad method of that class. Overriding is a term used in object-oriented programming for when you replace one method with another. The following is an example that loads in an XML document: var xml:XML = new XML(); xml.onLoad = function() { // Do Something } xml.load("http://www.dannypatterson.com/XML/rss.xml"); When we created the XML object, it already had an onLoad method that didn't do anything. Sometimes these are known as abstract methods. An abstract method is designed to be overridden and is how events are handled in most of the built-in Flash objects. One thing to remember about this method of event handling is that you must deal with scope issues. The XML example is a good way to demonstrate this. Let's assume that the following code is on your main Timeline. The code inside the onLoad method will be run in the scope of the XML object. Therefore, the keyword this references the XML object. You can demonstrate this by using the trace command inside the event handler like this: var xml:XML = new XML(); xml.onLoad = function() { trace(this); } xml.load("http://www.dannypatterson.com/XML/rss.xml"); You'll see in this example that this actually outputs the XML object. This isn't always the desired behavior. What if you wanted the event handler to execute in the scope of your main Timeline? One approach is to create a function and assign it to the event handler like the following example: this.onXMLLoad = function() { trace(this); } var xml:XML = new XML(); xml.onLoad = this.onXMLLoad; xml.load("http://www.dannypatterson.com/XML/rss.xml"); If you execute this code you might expect the output of this to be a reference to your main Timeline. But it isn't. It still references your XML object because the assignment of your onXMLLoad() function to the onLoad method was just a function reference. To get your onXMLLoad method to be called in the scope of the object it belongs to, you must use a utility class provided by Macromedia. The utility class is called Delegate and comes shipped with Flash 8. The following example shows you how to use it: import mx.util.Delegate; this.onXMLLoad = function() { trace(this); } var xml:XML = new XML(); xml.onLoad = Delegate.create(this, onXMLLoad); xml.load("http://www.dannypatterson.com/XML/rss.xml"); Now when you run this code you will see that your trace statement outputs a reference to your main Timeline. The Delegate class has fixed our scoping issue. There are many built-in classes that use event handlers, or callbacks. The following is a list of the classes and their callback methods:
There is one major limitation to this method of event handling. It is a one-to-one event communication. What if you had two objects that needed to respond to the XML onLoad event? You would need to create a handler that notified both those objects individually. This is a difficult work around. The real solution is listeners. We'll discuss those in more detail later in this lesson. |