BIT-101 [2003-2017]

How to Write Timeline Code


For any major project, you couldn’t pull me away from classes and an external editor. Nowadays it’s Eclipse and Flex Builder. Before that it was FlashDevelop, and before that PrimalScript. In general, I hate coding in the Flash IDE.

But often enough, when I’m doing some quick experiment or proof of concept, nothing beats opening up Flash creating a new movie, slamming some code down on the timeline, and pressing Control/Command-Enter. No concern about workspaces and projects. You don’t even have to save the damn file. “untitled-1.swf”! Amen!

Half the time I do that, that’s as far as it goes. But sometimes those quick experiments pan out into something I want to develop further. Then I’m left with the task of scraping that timeline code out of the Actions Panel, and turning it into a class. With my recent Mandelbrot experiment, I realized that there are things you can do to make that conversion process easier, if and when it comes.

Most of these go back to good practices I tried to follow way back in AS1 before I was even writing classes, and everything was on the timeline.

  1. All imports right up top.

  2. All timeline vars right after imports. Use var, and type your data.

  3. ALL code within functions. ALL code. NO stray statements on the timeline. Sometimes when you are just messing around, you’ll wind up with a mishmash of functions and statements. Gather up all those loose statements and put them into functions.

  4. All the statements that are just there to set things up go into an init function.

  5. Now you are allowed one statement on the timeline itself: init(); The stuff in the init function should be designed to run only once. “init” means “initialize”, which means, “set to the value or put in the condition appropriate to the start of an operation”. If you find yourself calling init() again, pull some of that stuff out into a “reset” function or something of the sort, and that’s what you call multiple times.

This leaves you with something like this:

[as]import flash.display.BitmapData;
import some.other.class;
import some.other.package.*;

var something:SomeType;
var somethingElse:SomeOtherType;

init();

function init():void
{
// body of init
}

function doSomething():void
{
// body
}

function doSomethingElse():void
{
//body
}[/as]

That’s timeline code, but hell, it’s almost starting to look like a class. Not only is it well organized and easy to read, but when the time comes to convert it to a class, there are a few simple steps to get it there:

  1. Wrap the whole thing in a package statement.

  2. Wrap everything after the imports in a class statement that extends MovieClip or Sprite.

  3. Add indentation to taste.

  4. Add private to all your vars and functions.

  5. Wrap the call to init() in a constructor. (Or remove the call to init, and turn the init function into your constructor. I prefer to keep the constructor as just a call to init.)

  6. You might have to add some more imports, as the IDE auto-imports a lot of common stuff.

If all goes well, you should have a working document class.

« Previous Post
Next Post »