Flash "/>

If in doubt - wait one frame

18 February 2006 at 3:27 am

I don’t remember who said it first, but it may have been Sam Wan at a Flashforward conference. Many things we do in Flash are based on timing. If a movieClip is not present just when you think it should be, you’ll run into trouble. You’ll run into this quite often when using Components. You instantiate a component, but it’s really not there until the Flash movie has gotten time to redraw. Here’s a utility class I’ve written to work around this problem. It’s really simple to use and it cleans up after itself. One could solve this using Intervals, but that comes with a whole new set of issues, so I prefer this way for now. (I have heard that there is a fix for cleaning up intervals though, but I haven’t seen that posted anywhere)

How to use the class.

At the top of the class you’ll find an example of how to use the script. You will also see what the four parameters are:

The first parameter is what to attach to. You will often use _root here, but you can specify any other valid scope. Next is the number of frames to wait. For most things, one is sufficient, but you may specify any number of frames. The third parameter is the name of the method to execute when the given number of frames have elapsed. The last parameter is the scope (location) of the method you just specified. Here’s a working example. Place this script in frame 1 on your _root timeline:

  s = new WaitXframes(_root,2,"test",_root);
  function test(){

Make sure you use the right captialization of the letters (WaitXframes). Save the file and copy the WaitXframes into the same folder as your FLA. After one frame, you should see the trace in your output window.