Publishers of technology books, eBooks, and videos for creative people

Home > Articles

Object-Oriented Programming with ActionScript 2.0: Effective Communications

  • Print
  • + Share This
Just like their real-world counterparts, effective communication between objects in OOP is a fundamental requirement for success. This article discusses the fact that in the object-oriented world, the entities that need to effectively communicate are the objects themselves.
From the author of

Effective Communications

Just like their real-world counterparts, effective communication between objects in OOP is a fundamental requirement for success. In the workplace, the required communications often take place between people: co-workers strategizing together, account managers working with clients, and so on. In these cases, a lack of communication can put a tremendous strain on the system (whether that system is an efficient office, vendor-client relations, or even software systems). In the object-oriented world, the entities that need to effectively communicate are the objects themselves.

Events as Inter-Object Communications

One type of communications that can occur between objects is the event. An event is any action that can be captured in code.

The prevalence of OOP has greatly increased in the past decade or so. This increase is directly related to the growth of the popularity of graphical user interfaces (GUIs) in modern applications. One key reason for this relationship is that object-oriented languages have a native ability to handle events.

User Events

The events most familiar to Flash developers and designers are driven by the end user. Each time a Flash application responds to the user's interaction, it is done through events. Note that each class in Flash has events to which it can respond.

System Events

Although many events are driven by the end user, some events are purely system events. These events react to specific application functionality, such as an external file being loaded or an error being thrown.

Handling Events

Any event triggered in an application, regardless of whether it comes from a user or the system, can be handled. Events are handled in one of two ways, either with listeners or with callbacks. In this article, we'll discuss how listeners are used.

Listeners are a basic means for handling events in object-oriented languages. A listener is an object that is notified when an event occurs. To create a listener, the object on which an event will occur (such as an instance of the MovieClip class) needs to be notified about which object will respond to its events. This is known as adding a listener to an object.

Listeners use a publisher-subscriber methodology, in that object A can subscribe to listen to object B. When object B broadcasts any messages, object A, as a listener, will be able to react to them. For a listener to react, it must have a method with the same name as the message being broadcast. Consider the example in Figure 1.

Figure 1Figure 1 The various departments of a consulting organization are set as listeners to the sales department; they are waiting for a message that a client project has been sold.

In this example, four people are set to listen to a single person. The billing, development, legal, and networking people are listening to the salesperson. They are waiting for an indication that a sale has been closed. When the salesperson broadcasts that he or she has closed a sale, each of the four listening persons has a series of tasks that needs to be done. Legal will create the contracts; networking will prepare the servers, and so on.

As it turns out, the salesperson has lots of different messages that may be broadcast, but these four listeners are interested in only one particular message, gotJob. For them to respond to only that message, each needs to have a gotJob() method, which can fire when the salesperson broadcasts the gotJob event. Any messages that are broadcast to a listener, but to which the listener does not need to have a response, will be ignored as long as the listener does not have a method matching the name of the broadcast message.

This same paradigm applies to objects. One object can be set to listen to another. Many built-in classes are designed with methods that enable other objects to listen to them. Any class with the methods addListener() and removeListener() are specifically built to enable external objects to listen for their events. Many of the native classes, such as Key, Mouse, and TextField, also have these methods.

To instruct one object to listen to an object that is broadcasting events, the broadcasting object's addListener() method is invoked. The addListener() method takes the listening objects as an argument. Listing 1 shows an example of an object listening to the Key class to capture any keys pressed by the user, and output the ASCII code for the pressed key.

Listing 1 An Object Is Assigned to Listen to the Key Class So That Any Key Pressed by a User Can Be Detected

var myListenerObject:Object = new Object();
myListenerObject.onKeyDown = function(){
   trace("The ASCII code for the key you pressed is: " + Key.getAscii());

  • + Share This
  • 🔖 Save To Your Account