If you've been using Flash for awhile, you probably already know what an "enterFrame beacon" is. If not, Flash MX 2004 provides one for you... you just have to find it!
How often have you created an empty MovieClip on the stage somewhere just to define an onEnterFrame for it? The onEnterFrame function is only available to MovieClips, but a lot of time there is a need to define an onEnterFrame for an generic object, so a MovieClip is created for that sole purpose. Instead of creating multiple MovieClip's and wasting some memory, worrying about depth management, and trying to shuffle things around, you should consider taking advantage on the OnEnterFrameBeacon class.
To allow any object to define an onEnterFrame that gets called everytime an enterFrame event occurs, check out the following code:
// in the transitions package? I think I would've put this in a util package...
import mx.transitions.OnEnterFrameBeacon;
// start the beacon, so we can listen for onEnterFrame
OnEnterFrameBeacon.init();
var o:Object = new Object();
o.onEnterFrame = function() {
trace("onEnterFrame outside of a MovieClip.. woohoo!");
}
// add the object as a MovieClip listener
MovieClip.addListener(o);
// you can also remove the listener as needed
// MovieClip.removeListener(o);
One thing to watch out for is that depth 9876 will be taken if you use the OnEnterFrameBeacon class. You can simply modify the class file and specify a different depth, if you need to.
This could also be done with setInterval and passing an interval time that mimics the frame rate. However, the interval might eventually get out of synch, depending on how much the player virtual machine needs to adjust the frame rate.
Note that instead of using AsBroadcaster to broadcast the "onEnterFrame" message, internally the OnEnterFrameBeacon uses "mx.transitions.BroadcasterMX" which mimics AsBroadcaster in functionality.
The interface for BroadcasterMX is the same as you're probably already familiar with...
import mx.transitions.BroadcasterMX;
obj = new Object();
// initialize the obj that will be broadcasting events
BroadcasterMX.initialize(obj);
// create a listener to respond to broadcasted events
listenObj = new Object();
listenObj.onMessage = function (param1, param2) {
trace("got onMessage " + arguments);
}
obj.addListener(listenObj);
obj.broadcastMessage("onMessage", "test param1", "test param2");
// you can also remove the listener:
// obj.removeListener(listenObj);
.. and there you have it - two hidden gems that come already written for you in Flash MX 2004. I know a lot of people have been using their own solutions for awhile, but I wanted to point out the classes that Macromedia has already provided. Hopefully this makes someone's life a little easier. :-)

12 Comments
onEnterFrame outside of a MovieClip.. woohoo!
Wait, what about this part of the class...
var mc = _root.createEmptyMovieClip ("__OnEnterFrameBeacon", 9876);
mc.onEnterFrame = function () { _global.MovieClip.broadcastMessage ("onEnterFrame"); };
There's still an mc there...
Posted by: Caseyc | January 8, 2004 8:39 PM
Casey,
Yes, there is a MovieClip, but I think you missed the point of my post. Any object can have an onEnterFrame function by using an enterFrame beacon, not just objects that extend MovieClip. You don't need to worry about creating a MovieClip just to attach an onEnterFrame to it. I thought I made that obvious... and when I specified depth 9876, I assumed you would know that a MovieClip would take over that depth. Sorry for any confusion.
Posted by: darron | January 8, 2004 9:29 PM
Mmmmm, beacon.
Posted by: Vera | January 8, 2004 11:02 PM
Here's what I use:
http://www.jonasgalvez.com/code/frameloop
I find it somewhat cleaner... but that's just me :-)
Posted by: Jonas | January 8, 2004 11:29 PM
setInterval() anyone?
Posted by: Paul Neave | January 9, 2004 5:11 AM
Paul:
http://chattyfig.figleaf.com/ezmlm/ezmlm-cgi/1/80227
Posted by: Jonas | January 9, 2004 7:27 AM
MovieClip.addListner is but a dream...
Posted by: JesterXL | January 9, 2004 8:38 AM
Thanks so much for this article! It's exactly what I was looking for!!!
Posted by: Alex | February 12, 2004 1:50 PM
Hi,
How can I include this broadcast message in a AS2 class and pass a message to a movie.
Tks
Johnny
Posted by: johnny | July 13, 2004 12:10 PM
Stop posting code on your web sitee that doesn't work. I copied and pasted exactly. Doesn't work.
Posted by: jeff | March 24, 2005 1:00 PM
@jeff - I'm sorry the code didn't work for you. I try my best to make sure the examples that I post work copy and paste style. That being said, what code segment did you copy and paste? What about it didn't work?
I opened up Flash MX 2004 Professional and tried both code segments. I pasted the first segment into a new .fla file and saw the messages appear in the output screen. I tried again with the second segment and saw the "got onMessage" string appear in the output window.
I can only conclude from this that both of these examples *do* in fact work, and that the error must be somewhere on your end.
Posted by: darron | March 24, 2005 1:57 PM
Darron,
Could you please discuss the use of _global.MovieClip? I see in the class that it is being redfined with each init() call. Seems like this would screw up the use of the class with other intances.
Thanks,
Brent Bonet
DarbyMedia
Posted by: Brent Bonet | November 16, 2005 11:06 AM