actionscript 3.0 for journalists

Object Oriented Programming

Flash, like many modern programming languages, utilizes a convention called Object Oriented Programming (OOP). The premise of OOP is to treat code like everyday objects that a person could interact with. At the core of Object Oriented Programming is the idea of using objects. Objects are pieces of code that you use in much the same way you would use a physical object in the real world.

Let's draw an analogy to the English language. An object would be considered a noun. With nouns, we can:

  • Describe them with adjectives.
  • Perform physical tasks with verbs.

ActionScript is similar in this respect. With an object, we can describe its appearance with properties (these are like adjectives), or we can have our object perform a task using something called methods (these are like verbs). Almost every object in Flash has a set of properties and methods that allow you to perform some type of task.

Objects are derived from something called classes. For example, if an object was a car, then a class would be the factory it was made in. You can create multiple objects from a class (just like you can make lots of cars from a factory). Each of these objects are unique and are called instances.

classes and objects

MovieClips, buttons and dynamic/input text fields are all considered objects. This is why we give them instance names, to identify each one that is on our stage for ActionScript. While we have been creating text fields and buttons in Flash using symbols, in actuality we were creating objects from classes.

Object Properties

Properties are like adjectives in the English language. Just as you would describe the color or size of a car, you can use properties to describe an object in ActionScript.

As discussed in a previous lesson, ActionScript uses dot-syntax to separate an object name from its property or method. Take for example the following code which specifies the width of a text field:

response_txt.width = 300;

The first word of the code is the object that you are affecting (also known as the instance name). The period is a separator. In fact, if you were to type this code in to Flash, as soon as you typed in the period, a code hint box would appear listing all of the properties and methods that belong to the TextField class (it knows we are referring to a text field because our instance name has the _txt suffix).

code hint

As you can see, the TextField class has lots of properties, many of them for highly technical purposes. You can see a full list of properties on the Adobe AS3 Help Documentation.

One of the more notable properties of the TextField class is the text property, which specifies the contents of a text field. We just used this in our previous lesson example on functions. Here is an example of setting the text of a text field:

response_txt.text = "Here is some text.";

Using the .text property, we can easily set the text contents of a text field. This is one of the more commonly used properties with the TextField class.

Object Methods

Methods are like verbs in the English language, because the usually perform some type of action. In reality, they are simply function calls that are associated with our object. Methods are easily identifiable by the parenthesis at the end of the command.

Using our car analogy, a hypothetical method for a car class would be go() or stop() or even start(). In practice, we would put the instance name of the car first, then using dot-syntax we would include the method.


Now, this is not a real class or method. But the idea is the same. The inside of the parentheses can be used to include arguments, which is extra data that would describe how the method runs. Be careful, because in ActionScript 3.0, some methods require a certain number of arguments and if you don't include them, the program will throw an error.

Let's take a look at a real example of a method. We will use the TextField object we used in the previous example. One of the methods is appendText() which will append some text to what is already in the text field without replacing it. Here is how it would work:

response_txt.text = "Here is some text "; 
response_txt.appendText("...and add some more to the end");

Without the appendText method, we would have replaced the existing text in the text field. Another method is getParagraphLength() which requires one argument. The argument Flash wants is the line number of your text field where you want Flash to count the characters in a paragraph. Line numbers start on zero, so putting a 0 in the parenthesis would indicate the first paragraph in a text field.

This method is different that the others, because it returns information. It does not actually perform an action on the text field.

response_txt.text = "Lorem ipsum dolor sit amet, consectetur adipisicing."; 

Notice how we put the getParagraphLength method inside a trace command:


The reason is because getParagraph returns data – a number. So we can't just state the method by itself. We could store the returned value to a variable, or a variety of other tasks.

Object Events

The one aspect of object that hasn't been mentioned before is object events. Events are tied to event listeners. These are the types of events that an object will listen for before running a function. Here are a few examples from the TextField class:

responseText.addEventListener(TextEvent.TEXT_INPUT, report); 
responseText.addEventListener(Event.SCROLL, report);

In this code, Flash would run the report function (not shown here) if a certain event takes place. In the first line, Flash is listening for TEXT_INPUT. This occurs when a user types in a letter in an input text field. Perhaps you want to run a Flash script that will count the characters every time a user types in a new letter, this would be the event to use.

Events have their own classes, as you can see from the word TextEvent above just before the event type. They are associated with the main class (in this case TextField) but are a subset of a separate events category. Flash bundles the documentation so you can see what events are associated with your classes.