Home / Articles / Adobe Flash / Introducing ActionScript Objects

Introducing ActionScript Objects

Contents

  1. Object Primer
  2. Object Sampler
  3. Video Tutorial

Chapter Description

Take advantage of ActionScript's objects, which allow you to perform all sorts of interactive tasks in some very dynamic ways. Also learn how to use objects' properties to manipulate data, colors, sound, dates, and more.

What you'll learn...

How objects are used in Flash Projects

The difference between global objects and classes of objects

About Flash object types and their uses

Just as various objects—for example, computers, cars, stoves, and vacuum cleaners—have unique functions in the physical world, each ActionScript object performs a unique role within Flash. These objects provide a means for working with text, sound, color, dates, and more—in short, allowing you to perform all sorts of interactive tasks in some very dynamic ways.

As you'll soon learn, ActionScript objects resemble their physical-world counterparts in that they have characteristics (or properties), which you can change via a script, and abilities (or methods), which allow them to perform various tasks. The primary advantage of using objects in ActionScript is that they allow you to program and manipulate data, colors, sound, dates, and so on, in a context that makes sense to humans. As you learn more about objects, you'll find that ActionScript's underlying logic becomes clearer as well.

In the text that follows, we'll examine the general structure of objects (that is, how they work), and then take a closer look at some of the specific ActionScript objects you can use to create interactivity in your own projects.

Object Primer

ActionScript objects provide a means of moving graphic elements dynamically, building applications, organizing data, and more. You accomplish these tasks by using or setting object property values and invoking object methods. In this section, we'll show you how properties and methods are used to create interactivity and explain how objects are organized within ActionScript—all of which should provide a strong foundation for the discussion of various objects that follows.

NOTE

This chapter is not meant to be an exhaustive examination of every ActionScript object.There are far too many (and their capabilities are far too diverse) to cover in a single chapter. Instead, we'll focus here on those objects that are likely to be most useful in your projects. For a more complete survey of objects, pick up a copy of Macromedia Flash MX ActionScripting: Advanced Training from the Source, published by Macromedia Press.

Properties

When you look at or touch a physical object, you're naturally aware of the various properties that define its overall form: size, weight, color, age, location, power requirements, and so on. The range of properties objects can possess is as diverse as the objects themselves, and the values assigned to these properties are what make them unique.

For example, a person can be defined by properties such as name, age, height, weight, hair color, and so on. If a person were to be described as an ActionScript object, these property definitions would look like the following:

person.name = "Matt";
person.age = 17;
person.heightInFeet = 6.2;
person.weightInPounds = 180;
person.hairColor = "blond"; 

Although Matt is a pretty well-rounded guy, changing any of these properties will affect either his appearance or the way he interacts with his environment. For example, if we were to change his name to Jill, not only would we likely upset him, we'd also need to change the way we addressed him; the name Matt would no longer elicit a response. Likewise, if we were to change Matt's height, he would no longer be able to reach things that he was able to before, and if we were to alter his hair color, his appearance would change considerably (take my word on this).

In ActionScript, many (though not all) objects have properties. For example, movie clip instances (which are MovieClip objects) have properties such as _rotation, _visible, _x, _y, and so on. The values of these properties define how the movie clip instance appears, and is interacted with, in your project.

Most property values can be set and read (used) by a script; however, others are read-only—which means that scripts can see and use their values, but not set these values. For example, all movie clip instances have a _totalframes property, which represents the total number of frames on that instance's timeline. While a script can use the value of this property (as in myVariable = _totalframes), it cannot set that value during playback, since the number of frames a movie contains is a hard-wired value (that is, it's locked in by Flash during the export process).

In Flash, property values are identified in relation to an object by a dot that separates the object's name from its property. Although you're probably already aware of this functionality, let's briefly review it. If you have a movie clip instance named bigDog, and you want to change its _rotation property, you would use the following syntax:

bigDog._rotation = 45; 

The above line of script will rotate bigDog 45 degrees. Notice that a dot (.) separates the object's name (bigDog) from the property you want to work with ( (_rotation). You would employ a similar syntax when using an object's property value. For example:

if (bigDog._x = your._x){
  gotoAndPlay("scream");
}else{   gotoAndStop("relax");
} 

In this script, an if statement compares bigDog's _x property value (horizontal location) with the value of your's _x property, and then reacts accordingly.

You'll notice from the script above that an underscore (_) is used in addressing some movie clip instance properties (specifically, those from the Properties book of the Toolbox List window) (Figure 14.1). This is a holdover from earlier versions of Flash, where properties were identified in this manner. However, in Flash MX, many object properties are identified without an underscore. For example:

myArray.length
myTextField.borderColor
mySound.duration 

Figure 14.1Figure 14.1 Properties listed in the Properties book of the Toolbox List window of the Actions panel are the only ActionScript properties identified with underscores.


As we progress through this chapter, we'll show you how you can use different objects' properties to create interactivity.

Methods

Methods represent tasks that an object can perform. For example, if you were to look at a dog as an object, its methods would include the abilities to eat, bark, run, play, and sleep. If a dog were an ActionScript object, the syntax for these methods would look like this:

eat();
bark();
run();
play();
sleep(); 

The parentheses that are included in a method are sometimes used to pass it a parameter or set of parameter values, allowing it to act in a unique way, based on those values. For example, if we wanted our dog to eat a particular type of food, we could indicate this in the parentheses, as in the following:

eat("steak"); 

If at a later time we wanted our dog to eat again, but something other than steak, we would once again invoke the eat() method, but pass it a different value, such as:

eat("chicken"); 

The method still does the same thing (eats), but what is eaten is different. Herein lies the power of methods: They perform specific tasks, but you can often configure them to perform those tasks using unique values. To give you a better idea of how this works, let's look at a couple of real examples using a movie clip instance. One of the methods available to movie clips gives them the ability to duplicate themselves: This is the duplicateMovieClip() method, which is invoked as follows:

myBankAccount.duplicateMovieClip("playMoney", 1); 

In the above example, we're duplicating the movie clip instance named myBankAc-count, naming the duplicate playMoney, and giving it a depth of 1. Notice in the example how a comma separates multiple parameter values used by the method. Also note that (similar to working with properties), the name of the object (myBankAc-count) and the method invoked are separated by a dot. You can invoke the duplicate-MovieClip() method in any number of ways. Take a look at one other example:

chocolateCake.duplicateMovieClip("goodStuff", 15); 

In this example, we've duplicated the movie clip instance named chocolateCake, given the duplicate an instance name of goodStuff, and a depth of 15. One method, many uses.

Not all methods take parameter values. For example, the stop() action/method doesn't, because its sole purpose is to stop a timeline.

Each type of object has its own unique set of methods—which makes sense given that each object type has a specific purpose and use in your project (Figure 14.2).

Figure 14.2Figure 14.2 Each type of object used in ActionScript has its own unique set of methods.

How Methods Function

If methods seem familiar, it's because they're based on functions (see Chapter 13). In fact, methods are really nothing more than functions that only have meaning in the context of the object type they're associated with.For example,the setVolume() method only has meaning within the context of a Sound object.This bond between function and object is what makes methods different. Note also that while functions are called,methods are invoked,which is really just geek-speak for the same thing.

Flash hides the code that defines methods for built-in ActionScript objects; however, if you were able to view their structure, you'd see that method definitions resemble function definitions. And once you understand how functions work, you can create your own methods or customize existing ones. Although these techniques are beyond the scope of this book, you can learn more about them in Macromedia Flash MX ActionScripting: Advanced Training from the Source, published by Macromedia Press.

Types of Objects

Not all objects are created equal. Some have a global bearing on your project (that is, on your project as a whole), while others let you create instances of them, which you can work with and control individually. In this section, we'll look at both types of objects, exploring their differences and explaining how you can work with each.

Global objects

Within the context of a Flash movie, some objects are global, or unique to the movie as a whole. The mouse, for example, is defined as a global object (Mouse object) because a Flash movie can only contain one mouse (cursor). You can't have more than one mouse, as you can movie clips, text fields, and sounds. Global objects are predefined within Flash, and as such are ready for use at any time. In contrast, objects such as movie clips, text fields, and sounds don't exist in your projects until you create instances of them (see "Objects with Instances" a little later in this chapter).

Flash contains the following global objects:

  • Math
  • Accessibility
  • Key
  • Mouse
  • Selection
  • Stage
  • System

You address these objects directly within ActionScripts, as shown below:

Math.round();
Mouse.hide();
Key.isDown(); 

Note the lack of instance names. Because all global objects are unique—that is, only one of each exists in a given Flash movie—instance names have no meaning in the context of global objects.

Object classes

A class of objects represents a group of object instances within your project that share the same basic structure and functionality. For example, all movie clip instances belong to the MovieClip class of objects; all text field instances belong to the TextField class of objects; and so on (Figure 14.3). When an instance of an object is part of a class, it shares the same properties and methods as all other instances of that class.

Figure 14.3Figure 14.3 An object class represents a group of object instances within your project that share the same basic structure and functionality.


For example, all movie clip instances have _rotation and _visible properties, as well as play() and stop() methods, which enable you to control them in various ways. These properties and methods are different than those that exist for instances of the TextField class of objects.

ActionScript contains dozens of object classes, including (but not limited to) the following:

  • Arrays
  • Numbers
  • Strings
  • Dates
  • Sounds
  • MovieClips
  • TextFields
  • XML

With the exception of MovieClip, Button, and TextField instances (which we'll discuss in a moment), all object-class instances are created via what's known as a constructor—a simple piece of ActionScript code that creates an instance of the object class you specify. For an example of this, take a look at the following:

mySound = new Sound(); 

The above line of code creates an instance of the Sound object class named mySound. To give you an idea of the common constructor syntax used to create all such object-class instances, let's take a look at another example:

//create an instance of the Color object
myColor = new Color();
//create an instance of the Date object
myDate = new Date();
//create an instance of the XML object
myXML = new XML(); 

Once an object instance has been created, you can target it for actions in scripts, attach event handler methods to it, or script other interactions with it. Take a look at the following example:

mySound = new Sound()
mySound.loadSound("heyJude.mp3", true);
mySound.onSoundComplete = function(){
   myMovieClip._visible = true;
} 

The first line of the above script creates an instance of the Sound object named mySound. The next line dynamically loads an external MP3 file into this object. The last three lines attach an event handler method to it, so that it will call the unnamed function shown when the sound loaded into it completes its playback. The instance must be created first, in order for either of the other functionalities to work. It's important to maintain this flow as you work with object instances.

You can use one script to create an object instance (such as on a frame) and another (such as on a button) to script its functionality—as long as the instance is created first.

As mentioned above, MovieClip, Button, and TextField object instances are created differently than other object-class instances—that is, without a constructor. This is because these are the only object instances that you create and place directly on the stage. Thus, when you create a text field on the stage, you're actually creating (by default) an instance of the TextField object. Likewise when you place a movie clip on the stage—you're actually creating an instance of the MovieClip object. When creating these instances, you assign them instance names, which allow you to work with and communicate with them via ActionScript.

NOTE

Object instances are often simply referred to as objects.

Objects are organized in the Toolbox List window in the following groups (Figure 14.4):

  • Core. These objects deal with internal data storage and manipulation.

  • Movie. These objects deal with visual content and system-related information such as movie clips, text fields, the stage, and accessibility.

  • Client/Server. These objects are used for moving data in and out of Flash.

  • Authoring. These objects allow you to create custom actions and components.

Figure 14.4 Figure 14.4 Objects are organized into four groups in the Toolbox List window of the Actions panel: Core, Movie, Client/Server, and Authoring.

2. Object Sampler | Next Section