How to communicate between loader and child

This post is about communication between loader (Parent.as) flash object (.swf) and loaded (Child.as) flash object. While flash is event based, I find it correct to use events to communicate, although different methods exist (direct function call etc.)

Here is our Parent.as class (goes Parent.swf). It loads Child.as on init and listens for events on it Child. It contains green circle pseudo button:

package
{
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.net.URLRequest;
    
    public class Parent extends Sprite
    {
        private var loader:Loader = new Loader();
        
        public function Parent()
        {
            var request:URLRequest = new URLRequest("Child.swf");
            
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, complete);
            loader.load(request);
            addChild(loader);
            
            addEventListener("hallo", hallo);
            
            var btn:Sprite = new Sprite();
            btn.graphics.beginFill(0x00ff00);
            btn.graphics.drawCircle(0, 50, 50);
            btn.graphics.endFill();
            btn.addEventListener(MouseEvent.CLICK, click);
            addChild(btn);
        }
        
        private function complete(event:Event):void
        {
            trace("Parent::complete()");
        }
        
        private function hallo(event:Event):void
        {
            trace("Parent::hallo()");
        }
        
        private function click(event:MouseEvent):void
        {
            trace("Parent::click()");
            loader.content.dispatchEvent(new Event("welcome"));
        }
    }
}

Child.as (published as Child.swf) dispatches event when added to stage and listens for other events from Parent loader. Contains black button:

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    
    public class Child extends Sprite
    {
        public function Child()
        {
            super();
            
            var btn:Sprite = new Sprite();
            btn.graphics.beginFill(0);
            btn.graphics.drawCircle(50, 50, 50);
            btn.graphics.endFill();
            btn.addEventListener(MouseEvent.CLICK, click);
            addChild(btn);
            
            addEventListener(Event.ADDED_TO_STAGE, added);
            addEventListener("welcome", welcome);
        }
        
        private function click(event:MouseEvent):void
        {
            trace("Child::click()");
            dispatchEvent(new Event("hallo", true));
        }
        
        private function added(event:Event):void
        {
            trace("Child::added()");
            dispatchEvent(new Event("hallo", true));
        }
        
        private function welcome(event:Event):void
        {
            trace("Child::welcome()");
        }
    }
}

Make sure you create Child.swf before you load it in Parent.swf. After you run Parent.swf, following is traced:

Child::added()
Parent::hallo()
Parent::complete()

after clicking button in Parent.swf you see:

Parent::click()
Child::welcome()

and click on button in Child.swf:

Child::click()
Parent::hallo()

The communication is event based, it is as easy as call dispatchEvent() with bubbling from Child or loader.content.dispatchEvent() from Parent:

In fact, loader.content is instance of Child.as root, so If you define listener directly on loader.content, you do not need to make dispatchEvent()-s bubbling.

To make it work online, make sure both .swf files are with the same securty sandbox and from same domain (or acceptable Security.allowDomain() set to make the other file access script).

4 comments so far

  1. Zach April 12, 2010 21:00

    This is awesome! I will definitely use this. Exposing random public function to the child always bothered me, because of it entanglement. This is great.

  2. pallzoltan June 8, 2010 10:11

    i’ve used loaderInfo.sharedEvents with the same results except for the security worry 🙂

  3. […] modules be able to communicate with the main application and vice versa. In fact I already did the approach some time ago, now it was time to improve it and implement into a flex […]

  4. Falkowski Andrzej June 9, 2013 23:31

    Thanx Jozef!
    very useful example
    AF

Leave a comment

Please be polite and on topic. Your e-mail will never be published.