From Flash to Flex: Moving to ActionScript 3
For years there has seemed to be a stigma in the development community about developing with AS (ActionScript). With the release of AS 2 there was a huge shift and many more developers made a transition to Flash; and with the ability to create classes, existing Flash developers were finally able to begin writing object-oriented code. AS has really matured over the years, and version 3 has raised the bar for object-oriented approaches by providing the ability to create packages, forcing strict typing through the compiler, and much more. In addition, the Flex environment has made it much easier to debug code and see errors or warnings while writing code.
The Flash to Flex series will focus on helping you migrate your work environment from Flash to Flex by learning the differences between and comparing the two applications. Last time, we covered how to integrate AS with MXML; this week we’ll take it a step further by focusing on AS 3 and how to use it to create packages and classes and then incorporate them into a Flex project. If you have questions about creating a new Flex project, incorporating ActionScript, or triggering events, take a look at my previous article, "Integrating ActionScript with MXML."
Creating a Package
The concept of a package did not exist in previous versions of AS. In fact, classes were not even possible until the release of AS 2, and in AS 1 developers were left creating prototype objects, much like in JavaScript. Thankfully, AS 3 has kept the same concept of classes from AS 2 and has also incorporated packages.
Packages are essentially the folder structure that you define for particular classes. For instance, my company is studiosedition.com, so the base package for all of my company classes will be the same as Listing 1.
Listing 1 Creating a base package
com.studiosedition
Let’s say that I have some utility classes, such as a class that only handles all alerts, named it Alert.as, and added it to a utilities folder. The full class path for this object would look like Listing 2 in AS 2.
Listing 2 Creating a full class path in AS 2
com.studiosedition.utilities.Alert
Listing 3 shows an example of how AS 3 enables developers to actually use the package keyword as other languages do.
Listing 3 Packages in ActionScript 3
package com.studiosedition.utilities { public class Alert { } }
A bonus of Flex is that it streamlines the creation of classes and packages by incorporating a dialog for creating new AS classes. The dialog enables you to browse for a package, name the class, choose whether it is public or internal, choose dynamic and/or final, browse for a superclass if it extends another class, and add interfaces. Beyond that it even provides the ability to automatically create the constructor, generate functions from an interface, and add comments. This helps save a lot of typing when you have a large project and are creating a lot of classes. Take a look at Figure 1 to see the dialog used to create a new AS class.
Figure 1 Creating a new ActionScript class with Flex
Note that the folder structure needs to exist in the Flex project in order to use it. For example, to use the package com.studiosedition.utilities, I needed to first create a com directory with a subdirectory named studiosedition, with another subdirectory named utilities.
Now that our class has been started we can take a look at how to start using it in the project.