A hidden gem... or two

| 12 Comments | 2 TrackBacks

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. :-)

2 TrackBacks

TrackBack URL: http://www.darronschall.com/mt/mt-tb.cgi/2

Darron Schall ïîêàçûâàåò, êàê âûçâàòü ñîáûòèå onEnterFrame, íå ñîçäàâàÿ äëÿ ýòîãî ìóâèêëèï. Òåõíèêà îñíîâàíà íà èñïîëüçîâàíèè âñòðîåííîãî êëàññà Flash MX 2004 mx.transitions.OnEnterFrameBeacon. Òàì æå -- �ðèìåð èñïîëüçîâàíèÿ êëàññà OnEnterFrameBeacon ä... Read More

FLASH from PukiWiki/TrackBack 0.2 on May 29, 2005 6:44 PM

FLASH/¥¤¥ó¥¹¥¿¥­¥Ã¥È FLASH/3D FLASH/AMFPHP FLASH/ASCBLibrary FLASH/FAME FLASH/Flash Shell Extension FLASH/framework FLASH/Library FLASH/silpentree FLASH/V2¥³¥ó¥�¡¼¥�¥ó¥È Source TECH LINK ʪ�ý¥·¥å¥ß¥ì¡¼¥·¥ç¥ó TEXT Tutorial Edito... Read More

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...

     
  • 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.

     
  • Mmmmm, beacon.

     
  • Here's what I use:
    http://www.jonasgalvez.com/code/frameloop

    I find it somewhat cleaner... but that's just me :-)

     
  • setInterval() anyone?

     
  •  
  • MovieClip.addListner is but a dream...

     
  • Thanks so much for this article! It's exactly what I was looking for!!!

     
  • Hi,
    How can I include this broadcast message in a AS2 class and pass a message to a movie.
    Tks
    Johnny

     
  • Stop posting code on your web sitee that doesn't work. I copied and pasted exactly. Doesn't work.

     
  • @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.

     
  • 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

     

Leave a comment

Flex.org - The Directory for Flex

Archives