updateAfterEvent() … What’s the Scoop?

ActionScript 2.0

Someone asked me the other day, “What’s the deal with updateAfterEvent()?  What, exactly, does it do, and when should it be used?”  Well, I’m a fan of the ActionScript 2.0 Language Reference, so my usual reply is along the lines of, “Let’s check out what the ASLR has to say,” but in this case, the documentation doesn’t speak the whole truth. 

The Scoop

First, what is this function?  In reference to this question, the Language Reference correctly states that updateAfterEvent() updates the display immediately after it’s called.  Normally, the display updates once per frame, so generally speaking, the faster your framerate, a) the smoother animation seems to occur and b) the more responsive certain mouse-related interactions feel.  This is easiest to see, for example, in dragging or follow-the-mouse animations that don’t rely on the native MovieClip.startDrag() method.  If the framerate happens to be slow, for whatever reason, such interactions can “stutter.”

Here’s a quick test.  Start a new FLA and draw a quick circle.  Convert it to a movie clip and give your circle an instance name; say, myClip.  Set the movie’s framerate to 1fps.  Yes, one frame per second.  Type the following ActionScript into a frame:

this.onEnterFrame = function():Void {
  ball._x = _root._xmouse;
}

Test, then drag your mouse around slowly inside the movie.  The “ball” will follow the mouse horizontally, but there will be nothing smooth about its motion.  Go ahead and change the framerate to the default 12fps.  Test again.  Still embarrassing, isn’t it?

The documentation goes on to say, still correctly, that updateAfterEvent() only works for certain Mouse and MovieClip events — and onEnterFrame isn’t one of them.  So it’s no surprise that the following does not improve matters (set the framerate back to 1fps to make this painfully clear):

this.onEnterFrame = function():Void {
  ball._x = _root._xmouse;
  updateAfterEvent()
}

But … change onEnterFrame to onMouseMove

this.onMouseMove = function():Void {
  ball._x = _root._xmouse;
  updateAfterEvent()
}

… whoa!  Way cool.  That’s a smooth response, and now we’re talking.

The Woops!

But wait!  The documentation states that “Flash ignores calls to updateAfterEvent that are not within an onClipEvent() handler or part of a function or method passed to setInterval().”  So this should only work for certain Mouse and MovieClip events, and even then, only when those are handled via onClipEvent() or a setInterval() loop.  Interestingly, we’re not in an onClipEvent() handler (see the Museum Pieces article for details).  Instead, we’re in the relatively newer “cousin” of onClipEvent(mouseMove), MovieClip.onMouseMove, and Flash hasn’t ignored the call to updateAfterEvent().

The Real McCoy

So … what’s the real truth?  ;)

According to a handful of slow-as-molasses 1fps tests, updateAfterEvent() works under the following circumstances only:

  • For the Mouse class:  the mouseDown, mouseUp, mouseMove, keyDown and keyUp events
  • For the MovieClip class:  the onMouseMove, onMouseDown, onMouseUp, onKeyDown, and onKeyUp events
  • The above pertains both to the Flash 5-era onClipEvent() function and the newer dot notation event handling
  • The above also pertains both to the setInterval() function and the “undocumented documentedsetTimeout() function

7 Responses to “updateAfterEvent() … What’s the Scoop?”

  1. NSurveyor Says:

    It’s good to hear that I have been using this correctly, though I have been told that I’ve used it improperly. :D

    Interestingly, I have seen “updateAfterEvent(mouseMove)” used though updateAfterEvent has no parameters and mouseMove is undefined. I’m curious as to whether it’s syntax was ever different, or maybe it’s just a mistake someone else made that spread: http://groups.google.com/groups/search?q=%22updateAfterEvent%28mouseMove%29%22

  2. David Stiller Says:

    NSurveyor,

    Interesting. Yeah, that sure doesn’t look right. Of course, there are always undocumented tricks — who knows how people discover them — but that one looks like an error.

  3. Tushar Says:

    Nice article. Thx.

  4. David Stiller Says:

    Tushar,

    Thanks!

  5. iShwaR Says:

    Hi David,

    I noticed with an updateAfterEvent() called within the function called by setInterval()… the performance of the flash movie becomes very smooth… but if you move your mouse while the movie is playing, the animation starts to jerk…
    for eg:
    place 5 balls on the screen with unique instance names.
    initialize all balls with random vx and vy.
    function moveBalls is called by setInterval every 1ms and all balls movement code is written here… and also theres an updateAfterEvent() within this function…

    on executing the code… the animation looks very smooth… but only when we dont move the mouse… :-S

    Can you please help…?

    Thanks,
    -iShwaR

  6. iShwaR Says:

    Thanks David,

    I guess i have solved my problem… Its just that you get the jerk in the FLASH IDE player… when you run your swf in a stand alone player… thrs no jerk… :)

    Thanks,
    -iShwaR

  7. David Stiller Says:

    iShwaR,

    Glad to hear that!

Leave a Reply