ActionScript - Creating Events

Most programming languages provide some mechanism to attach events to objects and then to associate each event with a function call.

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 | Problem with the Help | Adding Events to a Class


In general, when an event occurs, Flash creates an eventObject.

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.

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

As I've said, this is pretty simple ... but the help is terrible.

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

These fragments are from a class that is in a package. These are from the calling program This is an alternate technique 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 -
URL: http:// / user / clemenzi / technical / Languages / ActionScript / CreatingEvents.html