In general, this is simple and straight forward ... however, until you understand it, the way Flash provides help and prompts makes this very obscure.
Basics
An eventHandler is a function that accepts an eventObject as a parameter and returns void.
The eventHandler is associated with a specific event (such as a mouse click) and a related object (such as a button).
The basic technique is shown below.
function FunctionName(eventObject:EventType):void { // Place your code here } SomeObject.addEventListener(EventType.EVENT_NAME, FunctionName);The specific events that can be used depend on the objects they are attached to. For instance, it makes no sense to attach mouse events to objects that are not displayed. Other objects have events for Start, Status, Complete, and the like. Because of how the help system works, it will take a while to figure out what goes with what. (Sorry)
Note that EventType.EVENT_NAME represents a string value ... but don't use a string - they are case sensitive and using the EventType.EVENT_NAME syntax will allow Flash to flag errors.
Problem with the Help
When programming, there are two methods to get class method assistance - you can click the plus icon or use the tree panel. Both of these show some of the properties and methods available for each class ... but typically only those defined in the top level class. To see the rest of the options, you need to know first that you need to look and also where to look. This is true of both interfaces and base classes.
I understand that limiting the number of selections makes the interface less cluttered ... but I have had to spend several months exploring and memorizing the object hierarchy. This is ridiculous, Delphi has always (since version 1) managed to provide this type of information. One of the reasons that it took so long to figure this out is because I am familiar with Delphi which does it much better.
Another part of the problem is that the help screen always opens so large that the bottom part of the scroll area is way off the bottom of the screen. To make things worse, resizing the display takes a very long time - this is an extremely bad design.
Adding Events to a Class
public class TFileIO_mc { private var FOnComplete : Function; public function TFileIO_mc() { loader = new URLLoader(); loader.addEventListener(Event.COMPLETE, completeHandler); } // end constructor private function completeHandler(event:Event):void { if (FOnComplete!=null) {FOnComplete(event)}; } public function get OnComplete():Function { return FOnComplete; } public function set OnComplete(name:Function):void { FOnComplete = name; } } // end classThese are from the calling program
var f:TFileIO_mc = new TFileIO_mc; f.OnComplete = testdone; function testdone(event:Event):void{ trace("**** - it works"); }This is an alternate technique
var f:TFileIO_mc = new TFileIO_mc; f.OnComplete = function xx(event:Event):void{ trace("**** - it works"); }The point is to put the more difficult parts in the package and to greatly simplify the code needed to use it.
Warning: If the two parameter definitions do not match ... Flash crashes, but only part way. The program disappears from the desktop and all unsaved work is lost. It even sends a crash message to Microsoft. But the crash is not quite complete, I still have to use Process Explorer to kill the process.
Delphi checks the function types at compile time and throws an error if they don't match. Perhaps the next version of Flash will fix this.
Author: Robert Clemenzi - clemenzi@cpcug.org