Are you leaning to ActionScript programming?

Do you need to write a bit more than just gotoAndPlay() and gotoAndStop()?
Or finally would like to try to make some animations via ActionScript and not in timeline?

Then please consider the following advices!

There are tips for ActionScript, though they should be interesting to AS2 and AS3 guys too.

1. Separate your code.

2. Order your objects in the library.

3. Have a naming standard.

4. Do reference the objects appropriately.

5. Always have the only one mainframe.

1. Separate your code.

In Flash IDE you may put your code in any clip or movie element of the stage. I know it’s pretty common to put a button press handler code to button clip using the code similar to:

on(press)
{
   getURL("http://flashdaweb.com");
}

Yes, it’s pretty clear, but only when developing a simple 5-10 buttons menu for a website.
When getting into more complex flash works with a complex hierarchy and nesting you will get lost in your movie for sure if you will need to get back to it in a week or two.

So, accept it as a rule. Do ALWAYS create a separate layer and name it ‘actions‘. This should be the only place for all your actionscript code.

As said Bill Perry of Macromedia:

I throw a flash movie away if I don’t fine the actions layer. Instead actions in this file has been written in bits in more than 20 places!”

Here is an example of the way to put the code in there:

// fdwButton is the name of your clip placed at the Stage

_root.fdwButton.onPress = function()
{
     getURL("http://flashdaweb.com");
}

and so on with all other elements.

I believe this will simplify your future efforts when you’ll have all code in one place and will not need to browse through all the elements in timeline when something should be changed.

2. Order your objects in the library.

That will save you a lot of time if you will keep your object in an organized way.
Sort them in folders. And give all elements logical names. No more “symbol1” and “symbol2”.
Also, remove all unused objects from there as soon as they become useless, otherwise your compiled movie clip will be much larger because of garbage inside.

3. Have a naming standard.

Give the names for your actionscript objects and functions in a proper pattern, and keep doing this way all the time.
This will help you to understand the code. And will be much of thankful words if you’ll need to pass your project later to another actionscript developer

4. Do reference the objects appropriately.

Never ever mess the namespaces! If you’re using _root as a reference to some objects\clips, then use it for all the other clips. Try to do less referencing through this. or _global. in that case.

I would suggest you to create an empty objects as a namespace holders if there are a lot of stuff to organize. E.g.

//initializing
_root.buttons = new Object();
_root.images = new Object();

//usage
_root.buttons.myButtonsRelatedVar = 1;
_root.buttons.myButtonsClipLoader = new MovieClipLoader();
//etc.

5. Always have the only one mainframe.

This of course shouldn’t be considered for banners, or simple animations.
But this rule should be followed when doing the code.
You see, if you have more than one frame in your main timeline, all the code will be executed again, creating new variable and refilling them with the values.
If you write a code and need some animations, do the animations as movieclips, and only then put the movieclips onto the only mainframe.

Hope this will be helpful for someone.

Please visit our book store for more tips and tricks described in good books that we recommend. Some of them are just must-haves for beginner AS developers.