The Event Pattern: Flash AS3

A major problem is the communication between different objects like Sprites and MovieClips, in particular when they are in hidden locations in a movie. While we can use the direct communication using parent, parent.root or something similar, this is not good, because we have a direct dependency. We want to avoid direct dependency and have only very loose contact. Fortunately, in Flash we have the event model, which we can make use of. In the following I show an example how we can make use of it for communication between different objects.

Our objective is to gather information from different sources and display this information in one and the same textfield. First we make an outline of what we want, which you can see here. We want to process information from different sources and display in one textfield. In the following the classes are shown.

The AbstractClass class

To make it simple we create one class with a button, the AddModul class, which triggers the increase of a counter and one class, the DeleteModul class which triggers the decrease of the counter. Those are the classes, which send the information to a receiver class, which has a textfield and will display the result. As you can see from the outline, we have a black box, which processes the information. This box, the AbstractClass class, is a superclass for all the objects. The class is shown here. Basically there is a getter and setter method, which is internal and can only be used by subclasses of the AbstractClass class. The setter method contains a line, which triggers an event. We need, however, an object for the event, which can only be used by the subclasses. This excludes the main timeline and the stage, which can also be accessed by any other object. Therefore, we create a Sprite, myObject, and make it an internal static variable, which means that it can only be accessed by the subclasses and there is only one instance possible. Further, we need to give the event a name, which we do in form of a static constant. Although this name can be used outside of the subclasses, without an object, which will listen to the event, we are safe. Now let’s turn to the DisplayModul class and see how we get the information from one of the other classes.

The DisplayModul class

The DisplayModul class codes for a Label component, which displays the result, when one of the buttons is pressed. Other than that there is only the listener for myObject. We can then get the getter method, which contains the new message. Finally, we need to look at the Document class,, where all the objects are placed on the stage. We can place any of the objects, wherever we want in the movie. As long as they are sublasses of the AbstractClass, we can always send the message. And this concludes the tutorial.

Passing variable to a child movie with event pattern

In the same way we can also pass variable values to a child movie. All we do is extend the Documentclass of the parent and child movie to the same Abstract class.

You can download all files here.

Leave a Reply

You must be logged in to post a comment.