ActionScript 2.0 is fully object-oriented programming (OOP) language that provides several benefits to programmers, especially when creating large-scale Flash applications or presentations. OOP encourages reusability. You reuse a class you created in ActionScript 2.0 by inheriting the functionality of that class in other classes. The modular nature of OOP lets you replace or swap components of an application without changing the application’s structure. Finally, the fundamental metaphors of OOP design closely mirror the natural world (objects that have internal states and can do certain things), which speeds and facilitates the application design process.
Like ActionScript, OOP languages are based on the concept of classes and instances. A class defines all of the properties that distinguish a series of objects—it defines a category of object. A class describes the properties (data) and methods (behaviors) for an object, much like an architectural blueprint describes the characteristics of a building. The
Date class is an intrinsic ActionScript class. Using the
Date class you can make many instances. Each instance, or Date object, can represent a different (or the same) date. For example, you can make a Date object to represent August 1, 2010 and another to represent August 2, 2010. Each object inherits the same functionality from the
Date class. However, each instance is an independent object much as two buildings built from the same blueprint are unique instances.
Classes define data types. When an application has a new logical classification for data that doesn’t fit any pre-existing data types, you can write a new class. For example, if you are building an application that showcases t-shirts then you can write a TShirt class that defines the characteristics of that data type such as color and style. To define a class, you use the
class keyword in an external script file. You can create an external script file in the Flash authoring tool by selecting File > New > ActionScript File.
As always, ActionScript 2.0 provides several powerful and familiar OOP concepts and keywords (such as
package) found in other programming languages, such as Java. The programming language lets you build program structures that are reusable, scalable, robust, and maintainable. It can also decrease development time by providing users with thorough coding assistance and debugging information. You can use ActionScript 2.0 to create objects and establish inheritance, and to create custom classes and extend the top-level and built-in classes in Flash.
Flash Basic 8 and Flash Professional 8 include approximately 65 top-level classes that provide everything from core data types (Array, Boolean, Date, and so on), to custom errors and events, as well as several ways to load external content (XML, images, variables, and more). You can also write your own custom classes and integrate them into your Flash documents, or extend the top-level classes and add your own functionality or modify existing functionality.
You write a custom class in an external ActionScript (AS) file and you can import it into your application when you compile the FLA file. Classes can be very useful when you build larger Flash applications because you can organize a lot of the application’s complexity in external class files. When you move a lot of the logic into a custom class, you can not only make the code easier to reuse, but you can also “hide” some of the methods and properties from other parts of the ActionScript code. This helps you prevent other developers on the team from accessing sensitive information, or changing data that shouldn’t be changed.
When you use a class, you can also extend existing classes and add new functionality or modify existing functionality. For example, if you create three very similar classes, you can write a base class (abstract) and then write three classes (implementations) that extend the base class. These three subclasses can add additional methods and properties, so that you don’t need to create three class files that all duplicate the same code and logic.
Another benefit of using classes is code reusability. For example, if you create a custom class that creates a custom progress bar using the Drawing API, you could save the progress bar class in your classpath and reuse the same code in all of your Flash documents by importing the custom class. For more information on setting the classpath, see About Importing Class Files and About Setting and Modifying the classpath.