Accessibility

Flash Article

Converting from ActionScript to ActionScript 2.0

ActionScript 2.0 Dictionary

ActionScript 2.0 Dictionary
Article 1 by William Sanders

Table of Contents

Note: The following article is an excerpt from the Macromedia Flash MX 2004 ActionScript 2.0 Dictionary from Macromedia Press.

If you're a true Flash master (and we're betting you are) you know that the real power behind the throne in Macromedia's wildly popular Web animation software is its programming language: ActionScript 2.0. Offering a more robust programming model and better object-oriented programming support than ever before, ActionScript 2.0 allows you to take your Flash MX 2004 designs to new levels—if, that is, you know how to make full use of it. This volume, from the folks behind the software, makes sure that you do! In this highly cross-referenced dictionary, you'll find detailed descriptions of every ActionScript operator, keyword, statement, action, property, function, object, component, and method. What's more, an invaluable chart will help you locate definitions for symbolic operators or methods even when you don¹t know the object or component class. To top it all off, Macromedia has rounded up a posse of Flash and ActionScript luminaries to contribute articles on a range of topics—from converting ActionScript 1.0 to ActionScript to using design patterns in Flash.

Developers who have used ActionScript from its inception have seen it grow in size and complexity from a few actions to a complete lexicon rivaling even the most developed scripting languages. It has eclipsed languages like JavaScript 1.5 in its scope and size. (ActionScript 2.0 will look very much like JavaScript 2.0, though.) In this latest version of ActionScript, Macromedia has made enough fundamental changes to warrant a change in reference from ActionScript to ActionScript 2.0. All previous versions of ActionScript, but most significantly the version released with Flash MX, are now called ActionScript 1.0.

ActionScript 2.0 can be called an object-oriented programming (OOP) language, whereas previous versions were more modestly referred to as an object-based programming language, and that was only with the Flash MX version. Such distinctions are perhaps more hair-splitting than necessary because OOP is as much as an attitude toward programming as it is the built-in features of a language used to generate OOP structures in a script. (See Article 5, "Understanding Object-Oriented Programming in Macromedia Flash MX 2004," by Jeffery Tapper) The change in ActionScript's form is due to the OOP embedded in the ECMA-262 standard, out of which emerged ECMAScript Edition 4—the proposal for Internet scripting languages. (The latest version of ECMAScript standards is referred to as "Edition" or "proposal" by ECMA. A shorthand for ECMAScript proposal 4 is ECMAScript 4.)

For those familiar with the early days of personal computers, you may remember that every different brand of computer had its own version of the BASIC programming language. Among others were Atari, Commodore, Texas Instruments, Timex-Sinclair, Apple, and IBM. A program developed on an Atari could not be run on an IBM. Instead of coming up with a standard for all PC BASIC languages, the different companies just went their own way until the only ones left were Apple and IBM (running Microsoft BASIC). When the Internet came along, Netscape developed a new language called JavaScript that ran on its browser. Microsoft then developed a slightly different version of JavaScript to run on its browser, Internet Explorer, and so the developers had to write different scripts for the two different browsers.

Wisely, the European Computer Manufacturers' Association (ECMA) encouraged that a standard be developed so that the Internet would not become the Tower of Babel that the early personal computer languages were. Because ECMA sets standards for a number of different computer-related tasks, they number them, and ECMA-262 represents the standard for Internet scripting languages, most notably JavaScript. However, instead of calling it JavaScript, they called it ECMAScript, and the current edition is ECMAScript 4.

The new version of JavaScript under development to conform with ECMAScript 4 is called JavaScript 2.0, and so Macromedia followed suit and called the ECMA standard-meeting version of ActionScript "ActionScript 2.0." (See http://www.mozilla.org/js/language/es4/index.html for the full standard.) To be sure, you will find differences between ActionScript 2.0 and the ECMAScript 4 standard. For example, Flash has certain built-in non-ECMA classes such as MovieClip and LoadVars because it remains the scripting language for Flash and not something more generic. Otherwise, the language is very close to the ECMAScript 4 standard.

So why bother to adopt a standard that has nothing to do with Flash? Well, ActionScript could either remain a language unlike any other, or it could adopt a way of programming that is familiar to other programmers. By taking the latter course of action, ActionScript can be learned readily by programmers who already know OOP languages such as C++ and Java. Likewise, because JavaScript 2.0 is taking the same route, JavaScript 2.0 programmers will be able to pick up ActionScript quickly as well.

The key to this new direction is the object-oriented model of programming, and by making ActionScript more OOP-like and suitable for object-oriented programming, ActionScript provides a stronger base for well-structured programs with code that is:

  • Reusable
  • Modular
  • Scalable
  • Maintainable
  • Secure
  • Robust

Beginning with Flash MX and the introduction of prototype structures, ActionScript began migrating toward the ECMASCript 4 proposal, and that journey is now more complete with ActionScript 2.0. So although the change from ActionScript 1.0 to ActionScript 2.0 is a major one, the result should not be wholly unfamiliar.

As you read the rest of the article, you will find a discussion of what is new with ActionScript 2.0 and tools related to the new ActionScript. First, the article examines the new tools and procedures that affect all ActionScript programming in Flash. You will learn about changes to the Actions panel and the addition of the Behaviors panel and integrated Script window. Also in the initial section you will learn about the new case-sensitivity in writing script and how to create classes and strong data typing. No matter what you're doing with ActionScript 2.0, these new procedures and tools will affect the results.

Second, you will find that several new terms have been added to ActionScript 2.0. Some of these terms are new statements, objects, or general properties. For example, MovieClipLoader is a new class. Other terms, though, are properties or methods added to existing terms, such as the Systems object's new methods and properties. In addition, this section shows whole new ways of doing things in ActionScript 2.0. One of the major additions is the use of a subset of Cascading Style Sheets (CSS) to style text.

Finally, the article briefly examines using ActionScript 2.0 with components. You will find very few of the Flash MX ActionScript techniques for styling components still extant. Using ActionScript 2.0, you will find other fundamental approaches to using components as well. For example, instead of using the setHandler() method, listeners are now employed as event detectors.

The purpose of this section is to expand on and more fully examine what you will find in the main dictionary. It also serves as a quick look-up of what's new in ActionScript 2.0. Because the bulk of ActionScript 2.0 is virtually identical to ActionScript 1.0, if you already know ActionScript, this article will show the critical differences and new procedures to be followed, which will save you time and frustration. When you finish this article, you should have a good understanding of the new direction ActionScript 2.0 takes and how to use and integrate it with ActionScript 1.0.

Case Sensitivity

In ActionScript 1.0, all code is case-insensitive. That is, a variable named alpha can be accessed as Alpha or aLpHa or any combination of cases that spell "alpha." However, in ActionScript 2.0, the code is case-sensitive. For example, if you type the code

var alpha="Call me, Alphie"
trace(Alpha);

instead of seeing "Call me, Alphie" in the Output panel, you will see "undefined." That's because Alpha is not defined, even though alpha is. If your ActionScript 1.0 files have not paid attention to case sensitivity, you will have to rewrite them if you want to convert your code into files that will run on the Flash 7 player. Otherwise, they will only be able to run on the Flash 6 player.

The change from case-insensitive to case-sensitive code is a crucial matter, and until you get used to being sure that all references, definitions, and assignments are case-sensitive, you are likely to encounter bugs in your scripts.

In the same way that ActionScript 2.0 requires you to pay attention to cases in naming and referencing, it has also tightened up on the way data types are employed. The next section explains the entire process of data typing, and although it is not difficult to understand, it requires you to pay attention to a detail not required with ActionScript 1.0.

Strong Data Typing

You may not have been aware of the fact that all data typing prior to ActionScript 2.0 uses weak data typing. That means variables and properties can be assigned different types of data at different times, and ActionScript is perfectly happy. That is good because if you want to change a variable from a numeric variable to a string variable, all you needed to do was assign the variable a string value. For example, in ActionScript 1.0 the following script works just fine:

var alpha = 521;
trace(2 + alpha); //Output shows: 523
var alpha = "Bloomfield Rocks!";
trace (2 + alpha); //Output shows: 2Bloomfield Rocks!

The code also works in ActionScript 2.0, but it's a fairly sloppy practice because the variable changes from one type to another. Also, none of the type checking will be in place. Using strongly typed data, however, you are required to include the type of data when you declare a variable. For example, the following declaration creates a numeric variable:

var priceItem:Number = 14.95;

The variable named priceItem is now a numeric (number) variable and cannot be a string or some other kind of variable unless it is retyped. ("Retyping" refers to providing a new data type and not keyboard efforts.)

However, if you enter

var item:Number = "Hot Dog";

you will encounter an error message because the assigned value "Hot Dog" is a string instead of a number.

If you are familiar with languages like Java, numeric variables are broken down into finer categories such as double and integer, but ActionScript 2.0 simply uses Number for any type of numeric value. You can assign the following data types in ActionScript 2.0:

  • Accorion Alert
  • Array
  • Binding
  • Boolean
  • Button
  • Camera
  • CheckBox
  • Color
  • ComboBox
  • ComponentMixing
  • CustomActions
  • DataField
  • DataGrid
  • DataHolder
  • DataSet
  • DataType
  • Date
  • DateChooser
  • Delta
  • DeltaItem
  • DeltaPacket
  • Endpoint
  • Error
  • Function
  • Label
  • LoadVars
  • LocalConnection
  • Log
  • MediaController
  • MediaDisplay
  • MediaPlayback
  • Menu
  • MenuBar
  • Microphone
  • MovieClip
  • MovieClipLoader
  • NetConnection
  • NetStream
  • Number
  • Object
  • PendingCall
  • PopUpManager
  • PrintJob
  • ProgressBar
  • RadioButton
  • RadioButtonGroup
  • RDBMSResolver
  • ScrollPane
  • SharedObject
  • Slide
  • SOAPCall
  • Sound
  • String
  • TextArea
  • TextField
  • TextFormat
  • TextInput
  • TextSnapshot
  • Tree
  • TypedValue
  • Video
  • Void
  • WebServiceConnector
  • Window
  • XML
  • XMLConnector
  • XMLNode
  • XMLSocket
  • XUpdateResolver

In addition, all built-in classes and all custom classes and interfaces can be data types as well.

Most of the new features, including strong typing and the use of classes and OOP, are best understood by seeing how they work in the new integrated Script window, a feature only in the Professional version of Flash. So we now will turn to a discussion of how to use this new editing tool to optimize your use of ActionScript 2.0.

Using the Integrated Script Window to Create a Class

To facilitate coding in ActionScript 2.0, there are now two different scripting windows. First, you have the familiar Actions panel, which is almost identical to the expert mode of the Actions panel in Flash MX. The second one, called the Integrated Script window, works very much like a text editor, such as Notepad. You can write code in it that is saved and used outside of the FLA or SWF file, and that code is addressed and used in the Actions panel. However, unlike Notepad, the Integrated Script window has many of the error-checking and formatting features of the Actions panel. In fact, most of the code editing features found in the Actions panel are duplicated in the Integrated Script window.

You can write external scripts in the Integrated Script window and bring them into the movie using the #include action. However, the key purpose of this new script editor is to create classes. Flash MX uses the prototype and the registerClass functions to create classes, but that method is a bit awkward and divergent from the way classes are created in most other OOP-based languages. So, now classes are created in separate .as files written in the Integrated Script window. A single class is all that can be created in a single .as file, and the .as file name must match the class name. Furthermore, the .as file must be in the same folder as the FLA file that uses the class. The information in the .as file is compiled into the SWF file. Generally, development takes place with the FLA, SWF, and .as file all residing in the same folder, as shown in Figure 1.

Class definitions saved as .as files must be in the same folder as the .FLA files that use the classes

Figure 1. Class definitions saved as .as files must be in the same folder as the .FLA files that use the classes.

When you define a class, you can make it part of the authoring environment by placing it in the Classes folder within the First Run folder. The full path in Windows XP, for example, is Program Files > Macromedia > Flash 2004 > en > First Run > Classes. So if you create a class you're likely to use in several different programs, you can save time by placing it in the Classes folder.

To see how to use the Integrated Script window and create and use a class in ActionScript 2.0, follow these steps to create a simple movie:

  1. Open a new Flash document and create a total of three layers, naming them from top to bottom Actions, Text field, and Background.
  2. In the Text field layer, add two dynamic text fields next to one another, naming the one on the left itemOut and the one on the right checkOut.
  3. In the Background layer, type the label Product over the left text field and the label Total over the right text field. Save the file as classItem.fla in a folder named ItemClass.
  4. Select File > New > ActionScript File and enter the following script in Listing 1.1:

    class Item {
    	var describe:String;
    	var price:Number;
    	var tax:Number;
    	function Item(describe, price, tax) {
    		this.describe = describe;
    		this.price = price;
    		this.tax = tax;
    	}
    function selectedItem() {
    		return describe;
    	}
    	function kaChing():Number {
    		return price += (price*tax);
    	}
    }
    

    Listing 1.1 Creating a Class

  5. Save the file as Item.as in the ItemClass folder. Note that the class name "Item" must match the name of the file. Figure 2 shows the completed Integrated Script window with the Actions panel ghosted in the foreground.

    All class definitions must be created in the Integrated Script window

    Figure 2. All class definitions must be created in the Integrated Script window.

  6. Click the classItem.fla tab at the bottom of the Integrated Script window to select the FLA document and open the Actions panel. Type the following script:

    var computer:Item = new Item("Whiz Bang", 1234, .06);
    itemOut.text = computer.selectedItem();
    checkOut.text = "$"+computer.kaChing();
    

    Figure 3 shows the FLA document window with the related code and stage. Note the tab in the upper left corner to toggle back to the Integrated Script window. (The Macintosh version of Flash has separate windows with no tabs.)

    Only the Actions panel can be used to instantiate the class developed in the Integrated Script Window

    Figure 3. Only the Actions panel can be used to instantiate the class developed in the Integrated Script Window.

  7. Save the script and press Ctrl+Enter to test the movie (Command-Return on the Mac). You should see the current value of the Item.describe property in the left window and the output of the Item.kaChing() method in the right window. Figure 4 shows the output of testing the movie. Note the tabs in the lower left corner of the figure.

After going through all that for the simple output and calculations, the new system for creating classes may seem to be a step backwards in both productivity and coding. However, one of the main purposes of OOP is to encourage reusable code. The Item class can be called by any script simply by placing the Item.as file in the same folder as the SWF file calling it. By creating a library of classes in this fashion, production time can be reduced considerably.

The output requires both the code from the .as file and the Actions panel

Figure 4. The output requires both the code from the .as file and the Actions panel.

Macromedia Flash MX 2004 ActionScript 2.0 Dictionary Copyright © 2004 by Macromedia, Inc. Notice of Rights All rights reserved.


About the authors

Macromedia has put together a team of Flash community leaders to author important articles of interest to those using ActionScript and ActionScript 2.0: aYo Binitie, Jeremy Brown, Randy Drisgill, Alistair McLeod, William Sanders, Jeff Tapper, and Steven Webster. Together these community leaders have contributed to numerous publications and have been mainstays at Flash conferences around the world.