Chapter 12. Introduction to ActionScript 3

When your Flash document is on your computer, you're in control. You can make it do whatever you want, whenever you want. But eventually, your creation has to strike out on its own. You won't be there to tell your animation what to do when someone clicks a button or to remind it to turn off the sound after the first three times through. You need to provide instructions to make your animation perform automatically—that is, automate it.

To automate your animation or make it interactive, you use ActionScript—Flash's built-in programming language—to act on, or script, the different parts of your animation. For example, you can instruct your animation to load a web page when someone clicks a button you've added, to start playing an audio clip at the beginning of a certain scene, to play your animation in reverse, to loop certain sections of your animation, and so on.

Flash calls the chunks of ActionScript code you attach to your animation actions, which is a great reminder that ActionScript exists to help your audience interact with your animation.

The first part of this chapter explains how ActionScript has grown up from a simple macro language for animations into a full-blown programming language. After that, the chapter introduces you to some of ActionScript's basic concepts, with examples each step of the way. Follow the examples and try some experiments of your own. Go ahead, you won't break anything. You're on your way to a whole new level of Flash animation.

ActionScript is a serious programming language. As explained in the box below, folks in cubicles use ActionScript to develop major programs—like ticket purchasing and reservation systems. ActionScript incorporates geeky programming concepts like variables, functions, parameters, and so on. Delve deep and you find the scripting object model (the internal, Flash-designated names of all the parts of your animation). But none of that will stop you from using ActionScript for your own needs. In fact, Flash has some great tools to ease you into programming, like the Actions Panel and Code Snippets introduced in this chapter. The visual nature of Flash gives you instant feedback, letting you know when your script works and when it doesn't. Combine those features, and you've got a great way to dip your toe in the programming waters. You can even apply the skills you gain with ActionScript to other programming languages, including that web developer favorite, JavaScript.

ActionScript is a great name for a programming language. All computer programs perform actions, but the cool thing about Flash and ActionScript is that those actions are so visible. You're not just "assigning a value to a variable," as you would in typical computer lingo, you're making the moon move across the sky, or playing a video clip, or turning up the volume of the music. ActionScript programming is satisfying because many of the actions it performs are so apparent.

In the earliest versions, ActionScript was sort of tacked on to the Flash animation machine, the way macro programming was added to early word processors and spreadsheets. You used drop-down menus and dialog boxes to move parts of your drawing around the stage. You could start and stop animations on specific frames using familiar programming techniques like loops and conditionals (more on those later). In short, you could create some pretty snazzy visual effects.

At first, programming and animation seemed a curious match, since artists and programmers often seem to be such different people. But when you think about it, programming and drawing are both creative activities. Just like the artist, a programmer needs imagination and a vision. And animation is a very programmatic visual art, complete with reusable chunks of action that branch off into separate scenes. Today, there are Flash artist/programmers responsible for both the artwork and the programming code in their projects. There are also large teams producing Flash projects where artists create the objects that the programmers animate.

ActionScript and JavaScript have a lot in common. They're both scripting languages, meaning that they're programming languages that run inside other programs. Specifically, ActionScript runs inside Flash, and JavaScript runs inside HTML, the code that Internet browsers use to display web pages. On top of that, ActionScript and JavaScript sprouted from the same programming language specification, ECMA-262.

Initially, programmers used both ActionScript and JavaScript in snippets to perform quick and easy chores. For example, in ActionScript, you'd write something like the following:

on (press) {
 startDrag(this);
}

You would literally attach code to a drawn object on Flash's stage. JavaScript uses similar chunks of code to control the behavior of buttons and rollover images. However, JavaScript is often interspersed throughout the HTML code that describes web pages. From a technical point of view, ActionScript and JavaScript are considered high-level languages because they're closer to human language than the 1's and 0's of machine language.

As human nature kicked in and Flash animations became more elaborate, ActionScript snippets got tucked in all over the place. As a result, if an animation didn't work as expected, it was hard to find the misbehaving code. It could be almost anywhere. ActionScript writers started to use more disciplined programming techniques, and new versions of ActionScript encouraged better programming practices. The idea of attaching ActionScript code to any old object became frowned upon. Instead, programmers tried to keep all their code in one place, usually a single layer in the timeline. At the same time, object-oriented programming was becoming more popular and better-defined in programming circles, from the Visual Basic coders to the C programming crowd. ActionScript added object-oriented concepts in version 2 and even more in ActionScript 3.

The ActionScript chapters in this book focus entirely on ActionScript 3. If you're already versed in one of the earlier versions, you may be pleased to know that Flash CS5 still supports ActionScript 1 and 2, but you can't mix code from ActionScript 3 with code from earlier versions in the same SWF file. The reason is that the Flash Player now includes two completely separate virtual machines (the software that interprets ActionScript code and turns it into actions). The original one runs ActionScript versions 1 and 2. A completely new virtual machine handles ActionScript 3 code, which tends to run faster.

If you're new to ActionScript, no problem. As the language of the future, ActionScript 3 is the version to learn. If you're an experienced ActionScript programmer, it's worth a little relearning to gain the advantages that ActionScript 3 gives you.