Introducing Flash Objects
In this lesson, you'll deploy a number of different types of Flash objects and manipulate them via ActionScript. You'll implement text fields, movie clips, buttons, and a TextArea component, which automates the process of creating scrollable text fields. While it's possible to simply dive in and start using these objects, it's best if you understand what's happening behind the scenes. In the long run, you'll have a clearer idea about how Flash works, and writing ActionScript will become much easier.
Anatomy of an Object
As Flash continues to evolve, it has become increasingly object-oriented in nature. Object-oriented programming (also known as OOP) is a large and intimidating topic for nonprogrammers. However, even if you understand only the gist of the theory behind it, many once-mysterious aspects of Flash become more comprehensible. You've made use of objects every time you've worked in Flash, whether or not you did any scripting. Flash's interfacetimeline, Library, stage, tools, and many panelsprovides a visual environment for implementing and manipulating the underlying objects that make Flash what it is.You can think of objects of as a cluster of information and capabilities residing in a single entity, which can be reused in many different ways. Think for a moment about buttons in Flash. All the buttons have four states (Up, Over, Down, and Hit). All the buttons notify Flash when they're being clicked, rolled over, or dragged. All are capable of causing something else to happen (for example, triggering a gotoAndStop() action). Flash has an underlying Button object with all these features built in. Each time you create a new button, you configure various parameters (such as the size and color of its Up state), and you make use ofbut cannot changeits built-in capabilities.NoteStrictly speaking, you can change Flash objects' built-in capabilities. You can often extend themthat is, add new capabilities and features to the objects. However, that's an advanced topic that requires advanced programming. The potential to extend existing objects notwithstanding, you still cannot remove their built-in capabilities and features. Objectsin Flash or elsewhereare complex entities, which comprise a combination of several different parts. Broadly speaking, these parts can be divided into three categories.Events: Notifications that something has happened with or to the object. For example, button events include pressing, releasing, and dragging over the button. A frame event occurs every time Flash enters a new frame, notifying Flash that a new frame has loaded. Events are often used to trigger some other action.Properties: Variable pieces of information. A button has both height and width properties. Every button has a height and width, but the values of the height and width vary from button to button. An object's alpha transparency and location on stage are also stored as properties.Methods: Behaviors that the object can do. You use methods to make a timeline play or stop. The now familiar stop() and gotoAndPlay() are both methods of the MovieClip class.Any object can have any combination of parts in any of these categories. An object might have five built-in properties, two methods, and no events, or just about any other combination. A large partif not the majorityof learning ActionScript is learning about the different objects available in Flash, and learning how to take advantage of their built-in events, properties, and methods.
Scripting with Flash Objects
To make use of an object, you must create an instance of it, often referred to as instantiating the object. Once it's instantiated, you can take advantage of an object's events, properties, and methods. Obviously, you can't specify a button's height and width unless the button exists. Thus, objects exist at two levels: in an abstract level where events, methods, and properties are defined but don't have any values (think of human DNA); and a concrete level of an individual instance of that object, with fully defined values (think of an actual person). Objects at the abstract level are called classes or object classes, while concrete versions are called instances, or often just objects.Flash offers different ways of instantiating classes. When you create a new movie clip (Insert > New Symbol), you create a movie clip object based on the MovieClip class. When you put the movie clip on the stage, you instantiate it further. Notice that there are different levels of instantiation. A movie clip symbol in the Library is an instance of the MovieClip class, while an instance of a movie clip symbol on the stage instantiates the movie clip symbol. Some types of objects don't require this two-level instantiation; for example, text fields don't need to be placed in the Library before they can be deployed on the stage.Now that you have an idea of what's going on underneath the interface, you can probably see that you can often accomplish the same task either visually in the Flash interface or via ActionScript. For example, you can create a movie clip instance by dragging an instance of a movie clip symbol out of the Library and onto the stage. Another way to create a movie clip is to use the attachMovie() method of the MovieClip object, which creates an instance of a movie clip symbol residing in the Library and places it on the stage. Either way, the result is the same: a movie clip instance is placed on the stage.Regardless of how they're created, all objects need unique identifiers. In Flash, these unique identifiers are called instance names. Visible objects on the stage, such as buttons and text fields, can be given instance names in the Property inspector. If you create an instance using ActionScript, you usually give the instance an ID when you create it. After it has an instance name, Flash ActionScript can see it, which means that you can manipulate the movie clip and learn about it (such as whether a user is dragging it, and if so, where) via ActionScript.NoteNot all objects are visible. For example, Flash's LoadVars class, when deployed, is an invisible data holder. This class's built-in methods enable developers to send and load data to and from a server script, such as a ColdFusion, ASP, or PHP script. It also has built-in events that notify Flash when a given piece of requested data is fully loaded. This object has no visual representation in Flash. To summarize, using Flash objects usually follows a general sequence:
- The object is created, often in the form of a symbol or component. Some objects are built-in, such as the MovieClip class and Flash components (like the TextArea component), while others can be user-defined.The object is instantiated (that is, a copy of it is generated and deployed) on the stage, where it also receives specific attributes (such as location, height, visibility, and so on).The object is given a unique ID, called an instance name.The object is controlled, manipulated, or read via ActionScript.