ActionScript 2.0 Language Details
ActionScript 2.0 Language Details
ActionScript 2.0 introduces several programming keywords and concepts, including classes, inheritance, and interfaces. These concepts comprise what is commonly known as object-oriented programming.
- To get an introduction to object-oriented programming, see Learning AS 2.0 > Classes > About Object-Oriented Programming and Flash > Object-Oriented Programming Fundamentals.
- For a list of ActionScript terminology, see Learning ActionScript 2.0 > Terminology.
Features of ActionScript 2.0 include the following:
- Familiar object-oriented programming (OOP) model. The primary feature of ActionScript 2.0 is a familiar model for creating object-oriented programs. ActionScript 2.0 implements several object-oriented concepts such as classes, interfaces, and packages that will be familiar to you if you’ve programmed with Java, or another object-oriented language.
- The OOP model and inheritance. This model is a “syntactic formalization” of the prototype chaining method used in earlier versions of Flash, such as Flash MX, to create objects and establish inheritance. With ActionScript 2.0, you can create custom classes and extend built-in classes of Flash.
Strict data typing. ActionScript 2.0 also lets you explicitly specify data types for variables, function parameters, and function return types. For example, the following code declares a variable named
userNameof type String (a built-in ActionScript data type, or class).
var userName:String = “Fred”;
- Compiler warnings and errors. The previous two features (OOP model and strict data typing) enable the authoring tool and compiler to provide compiler warnings and error messages that help you find bugs in your applications faster than was previously possible in Flash.
When you use ActionScript 2.0, make sure that the publish settings for the FLA file specify ActionScript 2.0. This is the default for files created in Flash 8. However, if you open an older FLA file that uses ActionScript 1.0 and begin rewriting it in ActionScript 2.0, change the publish settings of the FLA file to ActionScript 2.0. If you don’t, your FLA file will not compile correctly, and no errors will be generated.
Classes you create in ActionScript must be stored in separate ActionScript (AS) files; you can’t create classes in a frame on a timeline (frame script), or attach one to a movie clip or button instance. Some important points about class files:
- Only one class can be defined per AS class file.
- The name of the file that contains the class must match the class name.
- ActionScript class files must be located in a folder where the Flash compiler looks for classes.
- You can combine several classes in a folder to create a package.
- About Setting and Modifying the classpath
(Learning AS 2.0 > Classes > About Working with Custom Classes in an Application > About Importing Class Files > About Setting and Modifying the classpath)
- About Packages
(Learning AS 2.0 > Classes > About Object-Oriented Programming and Flash > About Packages)
- Best Practices and Coding Conventions for AS 2.0
(Learning ActionScript 2.0 > Best Practices and Coding Conventions for AS 2.0)
Choosing Between ActionScript 1.0 and ActionScript 2.0
When you start a new document or application in Flash, you must decide how to organize its associated files. You might use classes in some projects, such as when you are building complex applications, but not all documents use classes. For example, many short examples in the Flash documentation do not use classes.
Using classes to store functionality is not the easiest or best solution for small applications or simple FLA files. When you build a small project, it is often more work and effort to use classes or external code files to organize ActionScript instead of adding ActionScript within the FLA file. It is often more efficient to put ActionScript inside the document. In this case, try to put all your code on the Timeline on as few frames as possible, and avoid placing code on or in instances (such as buttons or movie clips) in a FLA file.
If you decide to keep all the ActionScript code within the FLA file, rather than placing it within a class that you import, this does not mean that you should necessarily use ActionScript 1.0. You might decide to put your code inside the FLA document and still opt to use ActionScript 2.0 with its strict data typing and its new methods and properties. ActionScript 2.0 also offers a syntax that follows standards in other programming languages. This makes the language easier and more valuable to learn. For example, you will feel familiar with ActionScript if you have encountered another language that’s based on the same structure and syntax standards. Or, you can apply this knowledge to other languages you learn in the future. ActionScript 2.0 lets you use an object-oriented approach to developing applications by using an additional set of language elements, which can be advantageous to your application development.
In some cases, you cannot choose which version of ActionScript to use. If you are building a SWF file that targets an old version of Flash Player, such as a mobile device application, you must use ActionScript 1.0, which is compatible with Flash Player for a number of devices.
Remember, regardless of the version of ActionScript, you should follow good practices. Many of these practices, such as remaining consistent with case sensitivity, using code completion, enhancing readability, avoiding keywords for instance names, and keeping a consistent naming convention, apply to both versions. If you plan to update your application in future versions of Flash, or make it larger and more complex, you should use ActionScript 2.0, classes, and version control, to make it easier to update and modify your application.
Migrating to ActionScript 2.0
The first thing to understand about updating ActionScript 1.0 code to ActionScript 2.0 code is that, in many cases, it’s not necessary. ActionScript you wrote in earlier versions of Flash continues to work in Flash 8 and Flash Player 8.
Migrating your code from ActionScript 1.0 to ActionScript 2.0 is beneficial mainly in the following circumstances:
- For a large-scale Flash application or presentation that contains a lot of code, using ActionScript 2.0 simplifies the development process and provides more debugging information than ActionScript 1.0.
- To upgrade OOP programs that you created originally in Flash MX or Flash 5, using ActionScript 2.0 makes maintaining and modifying the code much easier.
- You’re curious about OOP and want to learn about it in a familiar authoring environment.
Also remember that you do not need to write class files in order to leverage some of the benefits or capabilities of ActionScript 2.0 or V2 components. You can still write ActionScript 2.0 code (such as code that applies data type annotations to variables) using frame scripts on a timeline.
Understanding ActionScript and Flash Player
Although new versions of ActionScript and Flash Player are often released simultaneously, they are not always synonymous. There are distinctions between Flash Player and ActionScript versions that can be very important to understand when building Flash applications.
You can compile a Flash document using ActionScript 1.0 or ActionScript 2.0 to Flash Player 6, 7, or 8, and it will work correctly. That is because both ActionScript 1.0 and ActionScript 2.0 compile to the same bytecode (the code that the ActionScript interpreter in Flash Player reads.) However, there are new ActionScript APIs (functions, classes, properties, and methods) introduced in each new version of Flash Player. For example, the
loadMovie() method of the
MovieClip class works in Flash Player 6, 7, and 8. However, the
MovieClipLoader class works only in Flash Player 7 and 8. That means that even though you can use either ActionScript 1.0 or ActionScript 2.0 for any of those player versions, you have to be careful that the APIs you use are supported in the target player version.
Neither ActionScript 1.0 nor ActionScript 2.0 are case-sensitive. However, Flash Player 7 and Flash Player 8 are case-sensitive. That means that regardless of whether or not you use ActionScript 1.0 or 2.0 the variables
UserName are the same in Flash Player 6, but they are different variables in Flash Players 7 and 8.