ActionScript Overview: Chapter 1 - Learning ActionScript 3.0

by Rich Shupe and Zevan Rosser

This excerpt is from Learning ActionScript 3.0. Learning ActionScript 3.0 gives you a solid foundation in the Flash language and demonstrates how you can use it for practical, everyday projects. The book does more than give you a handful of sample scripts, defining how ActionScript and Flash work. It gives you a clear look into essential topics such as logic, event handling, displaying content, migrating legacy projects to ActionScript 3.0, classes, and much more. Written for those new to the language, this book doesn't rely exclusively on prior knowledge of object-oriented programming (OOP). Instead, it helps you expand your skillset by first focusing on clear, concise examples in the timeline, evolving into OOP examples over time-allowing you to choose the programming approach with which you are most comfortable.

buy button

While you likely know what ActionScript is and are eager to begin working with the new version, a brief overview of its development will give you some insight into its use—particularly related to Flash Player and how it handles different versions of ActionScript. This brief introductory chapter will give you a quick look at where ActionScript 3.0 fits into your workflow, and will cover:

  • What Is ActionScript 3.0? It's to be expected that a new version of ActionScript will bring with it new features. However, this version has been written anew from the ground up and is even handled separately from previous versions of ActionScript at runtime. This intentional splintering of Flash Player affords significant performance increases, but also brings with it limitations as to how multiple versions of ActionScript interact.
  • The Flash Platform. At the time of this writing, ActionScript 3.0 is the internal programming language of Flex and AIR (the Adobe Integrated Runtime application). Differences in compiling and environment-specific attributes prevent every file written in ActionScript 3.0 from working in every aspect of the Flash Platform, but the fundamentals—indeed the bulk—of the language is the same throughout.
  • Procedural Versus Object-Oriented Programming. A great deal of attention has been focused on the object-oriented programming (OOP) capabilities of ActionScript 3.0, and the power and robustness of the language really shine in this area. However, you'll be happy to learn that a move to ActionScript 3.0 doesn't mean that you must become an expert at OOP. It is still possible to use a structured collection of functions, which characterize procedural programming, to author ActionScript 3.0 projects. In addition, using Flash CS3, it is still possible to code in the timeline, rather than coding exclusively with external classes. If you prefer object-oriented programming, enhancements to ActionScript 3.0's OOP infrastructure make it more robust and bring it more in line with the features of other important, OOP-based languages (such as Java) and make moving between such languages a bit easier.
  • The Document Class. Object-oriented programming is not for everyone, but for those starting on this journey, Flash CS3 offers a simpler entrance to an OOP application by way of the Document class. An attribute of the Properties Inspector, you need only specify which external class is your starting point, and no timeline script is required.
  • Legacy Code Compatibility. Because ActionScript 3.0 cannot co-mingle with previous versions of the language in the same file, developing projects that support older code is a chllenge. We'll briefly introduce the issues involved, and discuss them in greater depth in a later chapter.

Section 1.1: What Is ActionScript 3.0?

Although the new version of Flash's internal scripting language contains much that will be familiar to users of prior versions, it's probably best to think of ActionScript 3.0 as entirely new, for a few simple reasons. First, a few things are quite different, such as the event model and the way assets are displayed. Second, subtle changes run throughout the language and require some attention until they become second nature. These are usually small concerns, such as a slight change in the name of a property.

Most importantly, however, ActionScript 3.0 has been rewritten from the ground up and uses a different code base than prior versions of the language. This optimization provides relatively dramatic performance increases, but it means that ActionScript 3.0 code cannot be mixed with prior versions of the language in the same file.

The newness of this version, however, shouldn't intimidate you. It's true that the learning curve for ActionScript 3.0 is steeper than for prior versions, but that is usually a function of its robustness more than one of difficulty. Typically, there is an adjustment period during which users must occasionally adapt to a slightly new way of doing things.

To help you get over any possible trepidation, here's a look at some of the highlights of the new features of ActionScript 3.0. Keeping these benefits in mind may help make it easier to accept change, particularly when that change may initially seem tedious or overly complicated. Select new features include:

More detailed error reporting
ActionScript 3.0 requires strict data typing of variables, arguments, function returns, and so on. This data typing is discussed in Chapter 2, but boils down to telling the compiler what kind of data you expect to be working with at any specific time. Data type checking was introduced in ActionScript 2.0 but was previously optional. The heightened data typing enforcement improves error checking and provides more information while coding to allow you to correct the problem. Further, ActionScript 3.0 now enforces static data typing at runtime. This improves data type reliability at runtime, and also improves performance and reduces memory usage because the data types are stored in machine code rather than having to be dynamically addressed at runtime.
Syntax improvements
Syntax issues have been unified and cleaned up throughout the language. For example, property names have been clarified in some cases, and have been made consistent by removing the occasional leading underscores, as you'll see in Chapter 3. Also, multiple, subtly different ways of approaching the same or similar tasks have been made consistent, such as when loading external assets (discussed in Chapter 13) or linking to a URL (as seen throughout the book).
New display architecture
The many previous methods to dynamically add something to the display environment are now consolidated. The new display list simplifies this process significantly and also makes it easier to change the visual stacking order, as well as parent, child, and sibling hierarchical relationships, of display objects. As a major change introduced by ActionScript 3.0, we discuss this at length in Chapter 4.
New event architecture
Still another example of improved consistency, all events are now fielded by event listeners—essentially listening for a specific event to occur, and then reacting accordingly. The new event model is also more powerful, allowing mouse and keyboard events to propagate through multiple objects in the display list. The event model is discussed in Chapter 3.
Improved XML handling
A formerly cumbersome process, working with complex XML documents is now a pleasure with ActionScript 3.0. Adopting the standard commonly referred to as E4X, ActionScript now treats XML objects in a much more intelligent and familiar manner. The new approach allows you to use the same dot syntax to string related objects together.
More text scripting options
New text-processing methods now allow for much finer control over text manipulation. You can now find the text of a particular line in a text field, the number of characters in that line, and the character at a specified point (such as under the mouse). You can also find the index in the text field of the first character in a paragraph, and even get the minimum-bounding rectangle surrounding any specific character. All these options not only make working with a text field easier, but also allow a tighter integration with the lines and characters in a field and their surrounding stage elements. Text is discussed in Chapter 10.
New regular expressions
Another boon to text handling is the new native support for regular expressions. Regular expressions are like text manipulation on steroids. Instead of manipulating only specific, known strings of characters, you can now manipulate text using wild cards, character types (numeric, alpha, punctuation, and so on), white space (spaces, tabs, returns), repeating characters, and more. A simple example of regular expression use can be found in Chapter 10.
More sound management options
ActionScript 3.0's new sound capabilities are among the most eye-catching changes to the language. On a practical level, they improve access to both individual sounds and to all sounds playing. Sounds are now placed into separate channels, making it easier to work with multiple individual sounds, but also funnel all sounds through a sound mixer for collective control. You can also now get the amplitude and frequency spectrum data from sounds during playback. Sound is discussed in Chapter 11.
New access to raw data
For more advanced needs, you can now access raw binary data at runtime. Individual bytes of data can be read during download, during sound playback, or during bitmap data manipulation, to name a few examples. These bytes can be stored in a large list and still be accessed quickly and efficiently. We'll show one example of this technique in Chapter 11 when discussing sound visualization.
New automatic scope management
In a programming language, the word scope is sometimes used to define the realm in which an object lives. A Flash asset, such as a movie clip, might be in one part of the Flash movie but not another. For example, a child movie clip might be nested inside one of two movie clips found in the main timeline. That nested movie clip exists within one clip but not the other. Its scope, therefore, is restricted to its parent. Programming structures have limited scope, as well, and the challenge is making sure you work within the correct scope when addressing those structures. ActionScript 3.0 greatly simplifies this by automatically tracking scope as you program.
Improved object-oriented programming
Object-oriented programming structures have also been improved in ActionScript 3.0 with the inclusion of sealed classes and new namespaces, among other things. We'll discuss aspects of OOP in this chapter, as well as in Chapter 6, and provide class-based examples throughout the book. New in ActionScript 3.0, all classes are sealed by default, allowing only those properties and methods defined at author time to exist in the class at runtime. If you do find the need to change classes at runtime—by adding properties, for example—you can still do so by making the classes dynamic. Additionally, namespaces, including the ability to define custom namespaces, allow finer control over classes and XML manipulation.

Section 1.2: The Flash Platform

It's important to note that this book focuses primarily on developing ActionScript 3.0 applications using the Flash CS3 Professional integrated development environment (IDE). However, ActionScript 3.0 is the programming language for other Flash Platform applications, as well—notably Flex and AIR (the Adobe Integrated Runtime desktop delivery application).

AIR projects can also include HTML, JavaScript, and PDF, but ActionScript 3.0 is a large part of its appeal and the language most relevant to this discussion.

This means that the scripting skills you develop in Flash CS3 will be largely applicable in other areas of the Flash Platform, extending your reach as a programmer. There are, however, some important differences to understand when examining the big picture of cross-application scripting. We'll give you just a few brief examples here to consider.

To start with, Flash and Flex have different compilers so there is no guarantee that your project will compile correctly in both applications. You can use Flex Builder (the Flex compiler) to compile code-only ActionScript SWFs without the Flex framework, and load them into Flash CS3-generated projects. You can also load Flash CS3-compiled SWFs into a Flex project. However, as soon as you depart from core language needs, things start to get sticky.

For example, Flex does not have the resources of the Flash IDE to create visual assets (such as movie clips) and, by the same token, Flash does not support the Embed tag used by Flex to include such assets. This means that the same code cannot always be used seamlessly when such custom visuals are required. Similarly, the component architecture is different, including a different format and a component set that do not match.

This issue with visual assets has been a hotly debated issue for a while, and progress is being made to smooth the waters a bit. Adobe released a patch for Flex 2, and Flex 3 is in public testing at the time of this writing, improving the compatibility of components. However, it will probably be a while before moving code to and from these applications will be a comfortable process, if it ever happens. At a brisker pace, however, AIR development is becoming more of a crossover affair. Adobe is continuing to work on AIR authoring workflows that originate in Flash CS3.

The thing to keep in mind is that the ActionScript 3.0 language skills you develop will ease your move between applications in the Flash Platform, even if you must work with different authoring tools or compilers to end up with a finished product.

Pages: 1, 2

Next Pagearrow