|MySQL Conference and Expo April 14-17, 2008, Santa Clara, CA|
ActionScript for Non-Codersby Sham Bhangal, author of Flash Hacks
Author's note: In this article we'll look at how the Flash authoring environment allows an ActionScript coder to work and share ideas with designers who have no ActionScript experience, while at the same time allowing the designers to make full use of scripting. The article assumes Flash MX 2004.
To use Flash effectively, you do not just use the features available in the authoring environment. To be frank, such features are fairly basic, and you won't get very far using only the directly available drawing tools and ActionScript. In many cases, you have to create your own authoring building blocks using the default Flash tools, and create your application or site with the building blocks. Nevertheless, the great thing about the Flash environment is the way it is so open-ended, once you see how it is meant to be used.
Animating that Which Can't Be Animated
A while ago, Adam sent me an email. He wanted to animate some gnats.
He sent me the image (see Figure 1) to illustrate his problem.
He had a forest with a light source illuminating them from above, and to show this directional light, he wanted a small swarm of gnats at the bottom of the forest reflecting the light.
To do this with traditional tween-based animation is problematic. You need a layer full of tweens for each gnat, and to get a good-size swarm, you need an awful lot of tweens.
This reminded me of the time I went to Flash Forward (a large Flash developer conference), and a particular developer was speaking, telling us about a text effect he had spent the whole summer creating with tweened letters. He went on to learn ActionScript and realized that text effect was really a day's work (because in ActionScript, you can create a script that works for one letter, and then apply it to all your letters, thus quickly solving the problem).
(Incidentally, that's also why there are a few pages in the Flash Hacks book telling you all about creating a general text-effects engine in ActionScript.)
Summer is for the sun.
So anyway, I emailed Adam back and said, "Hey, Adam, that's a particle effect. You can do this quicker with a script. How often will you need to do this?" He told me it was one of the things that really slowed him down, because he needed to do particle effects all the time, and they take forever with tweens.
It is not just gnats either. A refection of the moon over water also takes a long time to do with tweens, but it can be done quickly with a few lines of code, because with a script, all the reflections can be controlled via the same code (see Figure 2).
Because Adam said he needed to create particle effects so often, and because he is an animator who doesn't use ActionScript, I decided to create a component for him. A component is a self-contained and configurable movie clip that you can just drag-drop onto the stage, configure, and you're done. Flash MX 2004 has a vast set of components that are based on a large, class-based framework, but you don't need to do anything so intense -- a few lines of code and half an hour of development time is also fine if that's all it takes.
Here's what I did to create the component (which you can have a look at by downloading it here).
Here, you define the values that can be added by the developer to configure the component. I created three number values:
Within the component are two layers, a layer called graphics (which holds the graphic for the rectangle), and a layer called actions. The latter layer is the important one, because it contains the following code, attached to frame 1:
This code does the following:
All an animator needs to do with the component is place it onto the stage and scale the rectangle. He then has to select the component and, using either the Properties panel or the Component Inspector, is able to configure the swarm by varying the values shown (as shown in Figure 4).
Once configured, all the animator has to do is test the movie. The rectangle will disappear, and in its place will appear the particle effect. Click on Figure 5 to see the Flash animation.
Of course, Adam was so pleased with never having to tween swarm animations again that he created a few animations using the component. Click on Figure 6 to see the Flash animation as a popup.
Although this effect is nothing new, the way I created some code encapsulated in a component for a non-programmer to include the code in their own work is one of the cool things about Flash. A team of animators/designers and one or two programmers can work together to create compelling content, without the designers having to learn coding, and without the coders having to learn the finer points of graphic design or character animation.