10 ActionScript Tips
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 ? Well, here are 10 of those for my loyal readers.
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
useWeakReferenceparameter of theaddEventListenermethod 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_FRAMEevent on that clip and watch iftotalFrameshas 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
Localeclass 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_STAGEevent to fire before trying to accessstage,rootandparent. For most developers that is now a given but my students always stumble upon this problem. - Use the
loaderInfo.widthproperty instead of thestage.stageWidthproperty when an swf is loaded within another one. - Use the labels
_up,_downand_overto automagically make a MovieClip behave like a button. With such labels, rolling over the MovieClip will make the playhead go to the_overlabel. ThebuttonModeproperty must be set to true in order for this to work. - Check
stage == nullto know if the current swf is loaded within another one. It works because the stage property is only available once aDisplayObjecthas 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
Keyboardclass instead of manually entering keycodes. For example, to check if the user pressed theENTERkey, you can useKeyboard.ENTERinstead 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!