Welcome to the companion website for Learning ActionScript 3.0 (O’Reilly). Ideally, you have found your way here after acquiring a copy of the book, and you are looking for further information about material referenced therein. If you’re new to this site, please continue reading. If you are a veteran to these pages, thanks for coming back, and have fun exploring.
Welcome
Back in late October of last year I started up a new website called ActionSnippet.com. I created the site because I really enjoy reading code - I often skip all the paragraphs in a blog post and just go straight for the code. A new code snippet is posted daily on ActionSnippet.com, usually with a brief description.
I would have linked to ActionSnippet.com sooner but I wasn't sure what direction the site would go in, so I wanted to give it some time to evolve before linking to it. After approximately 260 code snippet posts I can safely say there is much more useful/educational code on ActionSnippet.com than there is weird useless experimental code.
In this post I'd like to highlight a handful of useful snippets for everyday projects and I'll link to a few advanced graphical snippets. If you just want to dig right in, you can look at the all posts page which is just a list of every code snippet on the site.
As you read each of the following descriptions, consider opening Flash, pasting the code into timeline script and testing the swf.
FileReference.save()
-
var file:FileReference = new FileReference();
-
stage.addEventListener(MouseEvent.CLICK, onClick, false, 0, true);
-
function onClick(evt:MouseEvent):void {
-
file.save("some text.\nsome more text", "actionsnippet.txt");
-
}
This is one of my favorite features of Flash 10. We can save any kind of file to a user's computer using the FileReference.save() method. Its first argument is for the data to put in the file. This can be a String, ByteArray, or XML object. The second argument is the name of the file.
If you paste this code in your timeline all you need to do is click the stage and you'll get a dialogue asking where you would like to save the file "actionsnippet.txt".
I just got a newsletter with a shocking tidbit. Flash on the Beach is offering an amazing 48-hour recession-beater discount as a thank you to the Flash community. Until midnight on January 14th, you can get a full three-day pass to the Miami conference for only $99! Check the other conferences out there. That is a GREAT deal, and this is one of the best shows around. Peruse the following for more information:
To get this deal, go to this page and use the code "RSKNUTS1" at checkout.
UPDATE: The recession has hit everyone very hard and Flash on the Beach Miami is no exception. With everyone either forgoing travel and events, or waiting until the last minute to see how things play out, pre-registration was too low to proceed with the conference. John Davey, the event founder and organizer, felt that it was too risky to allow registrants to book possibly non-refundable flights, or to allow sponsors to commit funding, for fear of a possible later cancellation and the effect that would have on early adopters. As a gesture far too rare these days, he has given all early registrants not only a full refund, but a free pass to Flash on the Beach Brighton. Now that's class. We're sorry to see the event cancelled for 2009, but we sure are impressed at the way it was handled. Our sympathies to John for the iscal impact this decision probably had--another scar from financial world gone mad.
Grids: Image Gallery
Making image galleries in flash is very common. There are lots of different approaches, but the basic concepts stay the same. They're usually XML driven and they usually have external image files that are dynamically loaded into Flash. This post contains an image gallery shell that builds off of the Grids: Arranging Clips post. This post assumes you are familiar with that post, and with AS3 XML parsing. We won't be building this image gallery from scratch. Instead, I'll walk you through the key aspects of the code This post also contains source for the shell with some TweenLite animation added.
Flash on the U.S. Beach

I have been meaning to write about this for a while, but Flash on the Beach, the best Flash conference to date, is finally coming to the United States.
For some of us yanks, part of the charm of Flash on the Beach may be it's Brighton, UK location, and we won't lose that any time soon. There will still be a UK event a year from now. For others in our neck of the woods, traveling to England may not be affordable or may be a harder sell (psychologically) for employers, family, etc.--especially with the weakness of the economy.
Now, everyone can take advantage of these former liabilities by joining John Davey and his amazing crew in sunny Miami. Americans get home-field advantage, and everyone else gets to buy a few more rounds on the U.S. dollar.
There has never been a better time to commit to a Flash conference, for several reasons. Flash CS4 and Flash Player 10 are finally out, and bring with them a slew of new features and additional ActionScript classes to take advantage of. A lot of new projects are hitting, or soon to be hitting labs.adobe.com, such as Flex 4 (brewing nicely), Flash Catalyst (formerly Thermo), AIR 1.5, PixelBender, Cocomo, Alchemy (rock!), and much more.
Most importantly, perhaps, the big impact this economy is having on the job market means that being up to date, and networking with colleagues, peers, and even possible employers, has never been more important.
Those are some of the practical reasons you should go to Flash on the Beach. Some may argue that a better reason is that it's so much damn fun. Everyone from the conference host and brain trust, John Davey, all the way across the board to the volunteer Lifeguards, work really hard to make this an intimate, fun event. John puts a limit on how large the event can be, he works really hard on carefully picking speakers and shaping topics, and he plans nighttime events that are purely social so everyone can connect. I don't mean parties (of which there are nightly affairs) I mean "Inspired Sessions" where creative folk talk about their process and everyone gathers in one big theater to imbibe and enjoy.
Back to the capacity again, this idea has come up in discussion that John may impose a limit that he guards a little too aggressively. The past two years, Flash on the Beach has sold out well before it was scheduled to begin. That's positively awesome for those that get tickets early because you can really meet the folks you've learned from and read about, connect with friends, and focus on content. Unfortunately, it also means that a lot of folks are left out every year. I got a lot of emails asking if I could get people into my AS3 workshop this year--something I had little to no influence over, I'm afraid.
So, if you're thinking of going, be sure to act sooner, rather than later. Early-bird tickets are already on sale.
Here is a list of the speakers that have been announced so far (with more on the way). FotB usually gathers the best speakers from around the globe, and this event seems to be carrying that tradition forward.
- Mark Anders
- Carlo Blatz
- Lee Brimelow
- Rob Chiu
- Brendan Dawes
- Carla Diana
- Julian Dolce
- Joa Ebert
- Chuck Freedman
- Richard Galvan
- Hoss Gifford
- GMUNK Bradley Grosh
- Branden Hall
- Ralph Hauwert
- Robert Hodgin
- Elliott Hugh
- Mike Jones
- Mario Klingemann
- Koen De Weggheleire
- Lisa Larson-Kelley
- Seb Lee-Delisle
- Andre Michelle
- Stacey Mulcahy
- Chris Orwig
- Robert Reinhardt
- Rich Shupe
- Grant Skinner
- Geoff Stearns
- Craig Swann
- Jer Thorp
- Carlos Ulloa
- Dr. Woohoo!
You can even win a 3-day pass, if you have a blog or web site. Just display a linked event badge (see the badge on this page), let Flash on the Beach folks know about it, and you'll be entered into a drawing to win admission! Check out the info on the FotB site for more: Win a 3-day pass! (I wonder if we're eligible to win?)
I hope to see you there!
Flash CS4 still uses AS3
I guess this is answer questions week. I seem to be writing a series of posts (more coming) written to address questions from multiple emails or conference appearances. This has to do with AS4/ECMA 4. If you frequent any of the blogs, forums, lists, or other sources for current Flash info, this post will not contain any new information. However, if you're new to AS3 and have come to this site by way of our book, you will probably be reassured to know that Flash CS4 still uses ActionScript 3.
Flash CS4 does introduce some new classes and ActionScript features, but these are consistent with minor language updates that accompany most new authoring tool versions. Here's a short round up of links that Mike Chambers put together and the new CS4/AS3 docs.
ActionScript 3.0 will likely remain the primary scripting language of the Flash Platform for at least another 18 months, if not longer. (This is assumed because Adobe tends to release product upgrades approximately every 18 months). Furthermore, if the next version of Flash (or another Flash Platform app, such as Flex) introduces ActionScript 4, it will likely be an expansion of AS3. That is, AS3 is probably the last major language rewrite for quite some time, so now is a great time to invest in learning ActionScript 3.
On a loosely related note (and, again, nothing new for the community-aware), the recent rejection of the ECMA 4 proposal will likely have little impact on ActionScript. Here's a bit more on the subject, also from Mr. Chambers.
One of the questions about ActionScript 3 that I am most often asked is how to send arguments to a listener function along with an event. Recently, a reader named Jim asked just such a question.
To provide some background for this question, you could send argument data to a standard function like this?
function showMsg(msg:String):void {
trace(msg);
}
showMsg("Claire");
However, in an event listener, only one argument is allowed in the listener function: the argument responsible for receiving the event data. Using a standard mouse click listener as an example...
stage.addEventListener(MouseEvent.CLICK, showMsg, false, 0, true);
function showMsg(evt:MouseEvent):void {
trace("hello");
}
...the kind of question asked is, can you do something like this:
stage.addEventListener(MouseEvent.CLICK, showMsg, "hello",
false, 0, true);
function showMsg(evt:MouseEvent, msg:String):void {
trace(msg);
}
The answer is, not out of the box. The existing AS3 events do not provide for this capability. However, to answer Jim's question, the best way to pass arguments with an event is to create your own event class by extending AS3's Event class.
TweenLite Introduction
One of my favorite things to do in Flash is to program different types of motion. From particle systems to the boss at the end of an advergame—the basic principles for moving things around the screen with code are the same. There was a time when all the different types of motion that I saw happening—back in the Flash 4 days—were a complete mystery to me. I remember spending a long time reverse engineering open source examples from ultrashock.com.
Motion Myth
For some reason lots of people seem to think that ActionScript animation creates smoother animation than the Flash timeline. This is simply not true. The timeline is just as capable of creating "smooth" animations as ActionScript is.
On the timeline, if you want a smoother animation, use a framerate around 30fps and put more space between your keyframes.
ActionScript vs. Timeline
Students sometimes ask me when to use coded animation and when to use timeline animation. The answer is more complicated than you might expect. Here's a basic set of rules:
TIMELINE ANIMATION IS FOR:
- animation that is always the same (static animation)
ACTIONSCRIPT ANIMATION IS FOR:
- interactive animation
- random animation
- animation driven from an outside source like an rss feed
- complex, static physics simulations that would be hard to animate on the timeline
- complex interactive physics simulation
- time-based animation—that is, animation that always runs at the same speed independent of framerate
I like to make my custom UI elements easy to skin. If we wanted to create a check box class that was easy to skin, one thing we could do is create an FLA based AS3 component. Unfortunately to do that takes advanced programming skills. If you're an intermediate programmer you might want to be able to create a skinnable UI element without all of the AS3 component architecture.
One way to achieve this is by creating a custom class that uses Library elements as assets. To increase flexibility (for swapping skins, for example), you can create each graphical element of your check box, store them in your Library, assign a linkage class name to each element, and then pass those linkage class names into your custom class.
Grids: Arranging Clips
It's extremely common to need to position display objects in a grid formation. I usually find myself doing it when displaying thumbnails for an image gallery. This post will take you through the process step by step.


(57 votes, average: 4.04 out of 5) 


