Building applications with ActionScript
The process of writing ActionScript to build an application involves more than just knowing the syntax and the names of the classes you’ll use. Most of the Flash Platform documentation covers those two topics (syntax and using ActionScript classes). However, to build an ActionScript application you’ll also want to know information such as:
Options for organizing your code
You can use ActionScript 3.0 code to power everything from simple graphics animations to complex client-server transaction processing systems. Depending on the type of application you’re building, use one or more of these different ways of including ActionScript in your project.
Storing code in frames in a Flash Professional timeline
In Flash Professional, you can add ActionScript code to any frame in a timeline. This code executes while the movie is playing back, when the playhead enters that frame.
Placing ActionScript code in frames provides a simple way to add behaviors to applications built in Flash Professional. You can add code to any frame in the main timeline or to any frame in the timeline of any MovieClip symbol. However, this flexibility comes with a cost. When you build larger applications, it becomes easy to lose track of which frames contain which scripts. This complicated structure can make the application more difficult to maintain over time.
Many developers simplify the organization of their ActionScript code in the Flash Professional by placing code only in the first frame of a timeline or on a specific layer in the Flash document. Separating your code makes it easier to locate and maintain the code in your Flash FLA files. However, the same code can’t be used in another Flash Professional project without copying and pasting the code into the new file.
To make it easier to use your ActionScript code in other Flash Professional projects in the future, store your code in external ActionScript files (text files with the .as extension).
Embedding code in Flex MXML files
In a Flex development environment such as Flash Builder, you can include ActionScript code inside an <fx:Script> tag in a Flex MXML file. However, this technique can add complexity to large projects and make it more difficult to use the same code in another Flex project. To make it easier to use your ActionScript code in other Flex projects in the future, store your code in external ActionScript files.
Note: You can specify a source parameter for an <fx:Script> tag. Using a source parameter lets you “import” ActionScript code from an external file as if it was typed directly within the <fx:Script> tag. However, the source file that you use cannot define its own class, which limits its reusability.
Storing code in ActionScript files
If your project involves significant ActionScript code, the best way to organize your code is in separate ActionScript source files (text files with the .as extension). An ActionScript file can be structured in one of two ways, depending on how you intend to use it in your application.
Choosing the right tool
You can use one of several tools (or multiple tools together) for writing and editing your ActionScript code.
Adobe Flash Builder is the premier tool for creating projects with the Flex framework or projects that primarily consist of ActionScript code. Flash Builder also includes a full-featured ActionScript editor as well as visual layout and MXML editing capabilities. It can be used to create Flex or ActionScript-only projects. Flex provides several benefits including a rich set of pre-built user interface controls, flexible dynamic layout controls, and built-in mechanisms for working with remote data and linking external data to user interface elements. However, because of the additional code required to provide these features, projects that use Flex can have a larger SWF file size than their non-Flex counterparts.
Use Flash Builder if you are creating full-featured data-driven rich Internet applications with Flex. Use it when you want to edit ActionScript code, edit MXML code, and lay out your application visually, all within a single tool.
Many Flash Professional users who build ActionScript-heavy projects use Flash Professional to create visual assets and Flash Builder as an editor for ActionScript code.
In addition to its graphics and animation creation capabilities, Flash Professional includes tools for working with ActionScript code. The code can either be attached to elements in a FLA file or in external ActionScript-only files. Flash Professional is ideal for projects that involve significant animation or video. It is valuable when you want to create most of the graphic assets yourself. Another reason to use Flash Professional to develop your ActionScript project is to create visual assets and write code in the same application. Flash Professional also includes pre-built user interface components. You can use those components to achieve smaller SWF file size and use visual tools to skin them for your project.
Flash Professional includes two tools for writing ActionScript code:
Third-party ActionScript editor
Because ActionScript (.as) files are stored as simple text files, any program that can edit plain text files can be used to write ActionScript files. In addition to Adobe’s ActionScript products, several third-party text editing programs with ActionScript-specific capabilities have been created. You can write an MXML file or ActionScript classes using any text editor program. You can then create an application from those files using the Flex SDK. The project can use Flex or be an ActionScript-only application. Alternatively, some developers use Flash Builder or a third-party ActionScript editor for writing ActionScript classes, in combination with Flash Professional for creating graphical content.
Reasons to choose a third-party ActionScript editor include:
Some of the notable code editors providing ActionScript-specific support include:
The ActionScript development process
Whether your ActionScript project is large or small, using a process to design and develop your application makes work more efficient and effective. The following steps describe a basic development process for building an application that uses ActionScript 3.0:
You don’t necessarily have to follow these steps in order, or completely finish one step before working on another. For example, you can design one screen of your application (step 1) and then create the graphics, buttons, and so on (step 3) before writing ActionScript code (step 2) and testing (step 4). Or you can design part of it and then add one button or interface element at a time, writing ActionScript for each one and testing it as it’s built. It’s helpful to remember these four stages of the development process. However, in real-world development it’s more effective to move back and forth among the stages as appropriate.