Introducing ActionScript Classes, Objects, and Methods
Objects, classes, methods, and properties — whew, that’s a mouthful — are the building blocks of ActionScript programming. These terms are all related, and understanding how they interact is one of the keys to mastering ActionScript. ActionScript neophytes are often surprised to discover that they have been creating objects since they first began using Flash. In fact, everything you create in a Flash movie is an object. In a Flash movie, ovals, imported images, and blocks of text are all individual objects. You can also create your own objects by using ActionScript code. For example, you can create an instance of the Date object, which retrieves the current date and time from the computer used to view your Flash Web site.
Objects are identified by unique names and are also referenced by their location on a particular level. An object exists within a hierarchy in a movie and is called on not only by its established unique name but also by its location within the hierarchy. Objects may remain static through one part of the movie but change later. The Movie Explorer provides a graphical representation of your movie and the objects within it. The Movie Explorer (open it by choosing Window –> Explorer) represents the objects in your movie as a visual outline.
Objects have properties, and properties describe objects. An object’s properties are what make it unique. For example, you set properties for text objects with the Properties Inspector. When creating a block of text, you choose the font type, font color, and font size, all of which are properties of the text object. All of these properties contribute to making a unique block of text.
You can change many object properties with ActionScript code. For example, you can position an object on stage by modifying its _x and _y properties or change the height of an object by modifying its _height property.
A class describes everything an ActionScript programmer needs to know about an object. It might help to think of a class as information that defines an object’s properties, methods, and event handlers. Properties such as height, width, and position are often included in a class. The methods describe what you can do when using ActionScript with an object. For example, the getMinutes method of the Date object retrieves the current minute of the hour from the computer viewing your Flash Web site.
A class can be thought of as an object that makes other objects. When you create an object from a class, it is known as an instance of the class. To understand this, look at the Date object. The Date object is an ActionScript object that contains methods that retrieve the current date and time from the host computer. Before you can use any of the Date object’s methods, you must first create an instance of the Date object, like in the following code:
The first line of code creates an instance of the Date object, while the second and third lines of code use methods of the Date object (getHours and getMinutes) to retrieve the current hour and minute from the host computer.
If you are an experienced ActionScript user, you will probably agree that creating ActionScript in Flash 8 is more powerful and intuitive than ever. The new Script Assist feature takes a lot of the drudgery out of hand-coding ActionScript. This powerful new feature will benefit ActionScript veterans and beginners alike.