With experience, any developer will gather a bunch of useful tricks that are not easily found in books, sometimes undocumented and a bit cryptic or perhaps so obvious they’re easily overlooked. Wouldn’t it be nice if you could learn those tricks without having to go through the many painful hours it took to acquire them?
I’m not claiming to be an ActionScript 3 guru, far from that. I just learned from experience a bunch of useful techniques to solve specific problems. I’m starting to share them here in the hopes it can help others. If you have more, do not hesitate to use the comment form at the bottom to share them with the community.
So without further ado:
- Use the
useWeakReference
parameter of theaddEventListener
method to make sure you listeners are dereferenced when no longer needed. Some people say this encourages bad coding habits, I say it can’t hurt and may save your ass. - Use
addFrameScript(totalFrames-1, stop)
to stop a MovieClip instead of putting code on its timeline. You can also listen to theENTER_FRAME
event on that clip and watch iftotalFrames
has been reached. In Flash Player 11.3 you can now listen to label events which is even easier. - Use
unloadAndStop()
to properly unload SWFs (including stopping playing sounds which isn’t the case with the regular unload method). - Use the
Locale
class for i18n. You don’t even have to use the gui front-end that’s built into Flash. Just use the class. - Always wait for the
Event.ADDED_TO_STAGE
event to fire before trying to accessstage
,root
andparent
. For most developers that is now a given but my students always stumble upon this problem. - Use the
loaderInfo.width
property instead of thestage.stageWidth
property when an swf is loaded within another one. - Use the labels
_up
,_down
and_over
to automagically make a MovieClip behave like a button. With such labels, rolling over the MovieClip will make the playhead go to the_over
label. ThebuttonMode
property must be set to true in order for this to work. - Check
stage == null
to know if the current swf is loaded within another one. It works because the stage property is only available once aDisplayObject
has been put on stage (with the exception of the document class of a local project). - Pass multiple parameters to t
race()
if you need to. This method will happily accept an arbitrary number of arguments – ie :trace(var1, var2, var3)
– and spit them out in order. - Use the new
removeChildren()
method to get rid of all children of aDisplayObjectContainer
. For a long time, we had to do a while loop to remove all children. Since Flash Player 11 / Air runtime 3 this is no longer necessary.
Bonus :
- Place a function directly in the default package (outside of any class declaration) to make it available everywhere. It’s like adding a function to the language. Check out Arthur Debert’s printf library for a nice example.
- Use constants from the
Keyboard
class instead of manually entering keycodes. For example, to check if the user pressed theENTER
key, you can useKeyboard.ENTER
instead of searching for and manually entering the equivalent key code.
There you go. If you have more, please send them in the comments below. Cheers!
Awesome tips! thanks for this