If in doubt - wait one frame

18 Feb

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(){
    trace("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.

18 February 2006 at 3:27 am

4 Responses to If in doubt - wait one frame

  1. avatar photo
    Lee Harding 18 February 2006 at 7:05 am #

    Hi, this little script sounds perfect, and just what im loking for (im using intervals right now) - my knowledge of actionscript is not great though, i have tried using this script to no avail - can you point me in the right direction of how to use it… thanks for your time

    Lee

  2. avatar photo
    Jensa 18 February 2006 at 3:08 pm #

    Hi Lee,
    I’ve now updated the entry with an example. If you used the example from within the class, it was a capitalization error there. Also make sure your project exports to AS2 as this is an AS2 class.

    J

  3. avatar photo
    Rich Coppola 22 February 2006 at 3:57 am #
  4. avatar photo
    Randy 16 October 2006 at 8:49 pm #

    Thanks very much! Your little class was exactly what I needed to solve a frustrating problem.

  5.