-1

I'm trying to put together a projector of a sequence of externally loaded swfs and my general question will be as short as it can be.

If an external swf loaded into ctrl (an instance of MovieClip placed on stage during authoring) has in its first and last frames:

 dispatchEvent(new Event("FIRST_FRAME")); // in the first frame, and: 
 dispatchEvent(new Event("LAST_FRAME")); // in the last frame 

then - should those events be "heard" within the ctrl container?

At present I only seem to be able to listen to those events within the loaded content, not "higher", I mean - if I say in the loader complete listener:

mc:MovieClip = MovieClip(e.currentTarget.content);
mc.addEventListener("LAST_FRAME", function(){ // something });

then the events are heard, but not when I say:

ctrl.addEventListener("LAST_FRAME", function(){ // something });

The latter seems to be more robust, therefore I'm struggling to have it work, but I guess I've been missing out some lessons ;-) Has anyone been through this? Is my approach correct or should I take another path?

Cheers everyone.

user776686
  • 7,933
  • 14
  • 71
  • 124
  • So you have an swf that dispatches an event that you load into your current application, then you add that swf to a display object called `ctrl`? If so you need to enable bubbling when you dispatch the event like so `dispatchEvent(new Event("FIRST_FRAME", true));". – Taurayi May 29 '11 at 14:19

1 Answers1

0

You can get your crtl display object to recieve the event dispatched from your external swf by setting the bubbles option to true when dispatching your event. Look at the following example where SWFB.swf is loaded into SWFA.swf:

SWFB:

package swfb
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.TimerEvent;
    import flash.utils.Timer;

    public class Main extends Sprite 
    {
        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);

        }// end function

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);

            const SECOND:int = 1000;

            var timer:Timer = new Timer(5 * SECOND, 1);
            timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
            timer.start();

        }// end function

        private function onTimerComplete(e:TimerEvent):void
        {
            dispatchEvent(new TimerEvent(TimerEvent.TIMER_COMPLETE, true, true));

        }// end function

    }// end class

}// end package

SWFA:

package swfa
{
    import flash.display.Loader;
    import flash.display.LoaderInfo;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.TimerEvent;
    import flash.net.URLRequest;

    public class Main extends Sprite 
    {
        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);

        }// end function

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);

            var loader:Loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaderComplete);
            loader.load(new URLRequest("swf/SWFB.swf"));


        }// end function

        private function onLoaderComplete(e:Event):void
        {
            var loaderInfo:LoaderInfo = LoaderInfo(e.target);

            var container:Sprite = new Sprite();
            container.addChild(loaderInfo.content);
            container.addEventListener(TimerEvent.TIMER_COMPLETE, onContainerTimerComplete);
            addChild(container);

        }// end function

        private function onContainerTimerComplete(e:TimerEvent):void
        {
            trace("TIMER COMPLETE!");

            e.stopPropagation();

        }// end function

    }// end class

}// end package

SWFB.swf dispatches a Timer event 5 seconds after it's added to the stage. When it dispatches the event the bubbles and cancelable options are set to true.

In SWFA.swf the SWFB.swf is loaded into it and then added to a display object container called container. Then an event listener is added to container that listens for the Timer event from SWFB.swf to be dispatched. When it's dispatched the onContainerTimerComplete() event handler invokes the Timer event's stopPropagation() method to(as its name suggests) stop the propagation of the event.

Taurayi
  • 3,209
  • 2
  • 17
  • 15
  • thanks a bunch for your detailed lesson. I haven't thought of the two event properties, but this makes sense. Unfortunately I cannot dive into this job at the moment, but will try it the other day soon. Thanks again! – user776686 May 30 '11 at 19:22