ActionScript

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? 

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:

  1. Use the useWeakReference parameter of the addEventListener 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.
  2. Use addFrameScript(totalFrames-1, stop) to stop a MovieClip instead of putting code on its timeline. You can also listen to the ENTER_FRAME event on that clip and watch if totalFrames has been reached. In Flash Player 11.3 you can now listen to label events which is even easier.
  3. Use unloadAndStop() to properly unload SWFs (including stopping playing sounds which isn’t the case with the regular unload method).
  4. 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.
  5. Always wait for the Event.ADDED_TO_STAGE event to fire before trying to access stage, root and parent. For most developers that is now a given but my students always stumble upon this problem.
  6. Use the loaderInfo.width property instead of the stage.stageWidth property when an swf is loaded within another one.
  7. 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. The buttonMode property must be set to true in order for this to work.
  8. Check stage == null to know if the current swf is loaded within another one. It works because the stage property is only available once a DisplayObject has been put on stage (with the exception of the document class of a local project).
  9. Pass multiple parameters to trace() 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.
  10. Use the new removeChildren() method to get rid of all children of a DisplayObjectContainer. 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 the ENTER key, you can use Keyboard.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!

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *