Turning Animation into Code in Flash CS3

Flash CS3 comes with an exciting new feature that will make both developers and designers very happy. A designer will typically create animations using the Flash Timeline with keyframes and layers. This becomes problematic for developers, as they usually have to attempt to translate those animations into code so they’re dynamic. In CS3, designers can now instantly transform a Timeline animation into code that a developer can use. This is a huge step in helping designers and developers to work better together to produce the best possible output.

In this tutorial, we’ll create a simple motion graphics piece on the Timeline and convert into code, producing exactly the same results. [For a preview of this project, click here at www.layersmagazine.com/downloads.html. All files are for personal use only.]

STEP 1 Open the FLA File
Open Flash CS3 and from the Start screen, choose Flash File (ActionScript 3.0). The new animation feature that we’ll be using is only available in ActionScript 3. In the Properties panel, click the Size button to open the Document Properties dialog. Set the Frame Rate to 30 fps, change the Background to black, change the Size of the movie to 800 pixels (Width) and 600 pixels (Height), and click OK. Double-click the first layer in the Timeline, rename it “actions,” and lock it.

STEP 2 Import and Convert the Phone
Click the Insert Layer icon to create a new layer above the actions layer and name it “phone.” This will be the layer that contains the image of our cell phone. With the first keyframe selected, press Command-R (PC: Ctrl-R) to open the Import dialog. Select the image file and choose Import. Select the image on the Stage and press the F8 key to open the Convert to Symbol dialog. Choose Movie Clip, give it a name of “phoneClip,” and click OK. Select the phone Movie Clip and give it an instance name of “phone” in the Properties panel.


CREDIT: ©ISTOCKPHOTO/PAVEL KAPISH

STEP 3 Set Up the Timeline
Now we need to add some frames to the Timeline to hold the animation. Select frame 30 in both layers by clicking-and-dragging across the two frames, and then hit the F5 key to insert the new frames. Now select frame 1 on the phone layer. We’ll be creating an animation where the phone slides in from the left of the screen and then scales up. In the Properties panel, move the phone Movie Clip to x: –360. Now go to the Transform panel (Window>Transform) and scale down the clip to 50% on both the x and y axis.

STEP 4 Slide In
Select Frame 12 on the phone layer and press the F6 key to insert a new keyframe. Now select the phone clip and position it to x: 159 in the Properties panel. Control-click (PC: Right-click) between the two keyframes on the phone layer and choose Create Motion Tween. Set the Ease slider in the Properties panel to 100. Select the first keyframe on the phone layer and then the phone clip on the Stage. Open the Filters panel and click the plus sign and in the pop-up menu, choose Blur. Set Blur X to 100 and Blur Y to 0.

STEP 5 Make It Pause
Test the movie by pressing Command-Return (PC: Ctrl-Enter) and you should see a nice slide-in animation from the left of the screen. After this animation happens, we want to let the phone pause for a moment before scaling up. Select Frame 16 on the phone layer and press F6 to insert a new keyframe. This will give a 4-frame pause before the rest of the animation continues. There’s no need to create a Motion Tween because nothing is changing on these frames.

STEP 6 Scale It Up
Select frame 30 on the phone layer and press F6 to insert a new keyframe. Select the phone clip, open the Transform panel, and set the scale on both axes back to 100%. Control-click (PC: Right-click) anywhere between frames 16 and 30 on the phone layer and choose Create Motion Tween. Set the Ease slider in the Properties panel to 100. Now select frame 23 and insert another keyframe. Add some blur to the phone by going to the Filters panel and setting both the Blur X and Blur Y values to 15.

STEP 7 Finish Up the Animation
Test the movie and you should now see a nice animation effect. The last thing we need to do is to insert a stop action at the end of the Timeline so the animation doesn’t loop. Select frame 30 in the actions layer and press F6 to insert a new keyframe. With that keyframe selected, open the Actions panel (Window>Actions) and type in a stop command (as shown above). Test the movie again to see the finished animation.

STEP 8 Convert To Code
Now it’s time to convert your Timeline animation into ActionScript code. Select all of the frames on the phone layer by clicking-and-dragging a selection over all 30 frames. Control-click (PC: Right-click) on the selected frames and choose Copy Motion as ActionScript 3.0. This will open a window asking you for the instance name of the animation. Enter “phone” and click OK.

STEP 9 Paste the Code
Select the first keyframe in the actions layer and open the Actions panel. Paste the generated code into the editor by pressing Command-V (PC: Ctrl-V). You should now see 55 lines of ActionScript code—equivalent of the animation that we created on the Timeline. It’s a combination of regular ActionScript code and a new XML format that was created specifically for animation in CS3.

STEP 10 Delete Your Work
The ActionScript code that you pasted into the Actions panel is now going to handle animating our phone so we can delete all of the Timeline animation that we did previously. To do that, select all of the frames in both layers except for frame 1. Now Control-click (PC: Right-click) and choose Remove Frames to delete all of the selected frames. Test the movie now to see the code-driven animation. It should look identical to the Timeline animation that you created earlier.

STEP 11 Understanding the Code
On line 1 of the code, Flash is simply importing the necessary code library into our project so it’s available to us. A new XML object is created on line 2 which will hold all of our animation XML code. There are two main sections to this XML data: There’s some code that describes the object being animated; and then, starting on line 14, you can see all of the keyframe data that we created on the Timeline. After the XML code, a new Animator object is created using the XML, and the last line of code begins the animation.

STEP 12 Keyframe Data
If you look over the keyframe tags in the XML data, you’ll see that it has an attribute called “index.” This represents the frame number where that keyframe occurs. You’ve probably noticed that it seems to always be short by 1 frame—because in-code indexes always start at 0 not 1. All that’s required to change the timing of the animation is to adjust the index values; for example, you could change the index value on line 23 to 5 to double the speed of the animation.

STEP 13 Changing the Animation
You may notice that some values in the code seem different than they were on the Timeline. This is expected and it doesn’t signify that anything’s wrong with the code. Let’s make a change so that the phone slides in from the right instead of the left:

Go to the first keyframe tag on line 14, add a new x attribute, and set it to 1000 (as shown here). This will position the phone off to the right of the Stage at the beginning of the animation. Test the movie to see the change.

STEP 14 Next Steps
In this example, we really haven’t done much with our newly generated animation code, but there are endless tweaks that can be made to create all kinds of different effects. To learn about all of the possibilities, you’ll want to look in the Help files.

This generated animation code can now be delivered to a developer who can then make any or all of these values dynamic. Finally, designers and developers can work together easily in Flash to create compelling animations. Kudos Adobe!

Visitor Comments »

 

I know these days people have become forgiving on a certain level of the bloat of SWF files but I have to say that this simple animation done with code is much larger than the timeline version (17KB vs 29KB). It’s great that it’s dynamic but shouldn’t there be an in-between solution? When size is an issue, as it can be from a production standpoint, being stuck with the timeline can be annoying. I suppose creating your own classes is an answer but for something built-in I just assumed it would work just a little better.

 

Comment by christian | March 14, 2008 @ 3:41 pm

 

Thank you. All of this was very understandable. This was the exact thing I was looking for, and you nailed all of my problems!

Thanks again. Kudos to you.

 

Comment by Jay | March 29, 2008 @ 6:49 pm

 

nice, nice , nice….this makes coding look like a child’s play:)

 

Comment by Marto | April 7, 2008 @ 5:30 am

 

Looks like this feature will enhance interactivity between professionals in the field, which is very positive !!

Mike:)

 

Comment by Mike | April 11, 2008 @ 5:45 am

 

Is there anyway I can get the last couple of lines of code? I am doing an animation similar to this one and I can’t get the end of my code to work.

Thanks

 

Comment by Ashley | April 22, 2008 @ 11:21 am

 

Shame we can’t use it for another year until Flash 9 saturation!

 

Comment by nick | April 23, 2008 @ 4:13 am

 

re: christian

This does not have to be the form of the final output of the Flash movie. It is just so developers and designers can talk the same language.

In practice, this XML would be better used in an external file, and loaded. Then the XML could be loaded by a coder’s program, adjusted, saved, and used by the original Flash movie.

later,
Neal.

 

Comment by Neal | April 25, 2008 @ 1:28 am

 

THanks fot this tutorial! How do i make an external file of the animation code an apply it to other movieclips with different instance names?

 

Comment by Laurenz | June 10, 2008 @ 6:04 am

 

you are my god

 

Comment by sarmi | July 12, 2008 @ 10:12 pm

 

how do i get hold of the picture of the phone?

 

Comment by Shayne | October 5, 2008 @ 3:46 pm

 

lovely article .. thanks!

 

Comment by reggie | October 18, 2008 @ 1:40 am

 

Very well done, thank you for a very easy to follow tutorial.

Peter

 

Comment by Peter | October 18, 2008 @ 4:13 am

 

this is mucking afazing could you give some tips on mouse pos vs stage x and y pos…really need that…

 

Comment by Randall | October 29, 2008 @ 4:30 am

 

exactly what I’ve just spent hours searching for on the internet! spot on for moving an embedded video clip across the screen without making the clip jump back to the beginning.

 

Comment by sean | November 17, 2008 @ 4:29 pm

Leave us a comment

Comments RSS | TrackBack URI

Back to Top

 
 
Advertisement
We are hiring

Creative Suite Tutorials
  1. Photoshop Photoshop
  2. Illustrator Illustrator
  3. Indesign Indesign
  4. Dreamweaver Dreamweaver
  5. Fireworks Fireworks
  6. Premiere Premiere
  7. Flash Flash
  8. After Effects After Effects
  9. Lightroom Lightroom
  10. Acrobat Acrobat

Get the latest tips, tricks and news delivered straight to your inbox.

From our Partners
Subscribe to Layers Magazine
 
 
 
  • Back to the Layers Magazine Homepage
  • Creative Suite Tutorials
  • Layers Magazine
  • Reviews on top products
  • Layers Magazine Reader Forums