How to access parent movieclip's variable from loaded swf

chchrist picture chchrist · Nov 25, 2009 · Viewed 26.2k times · Source

I have a swf which load an external swf. The loader swf has a variable which i want to access from the loaded swf. I use MovieClip(parent.parent).myVar but it return this error

TypeError: Error #1009: Cannot access a property or method of a null object reference. at card01_fla::MainTimeline/frame1()

The loader swf has a custom Document Class which i think is the problem.

Thanks in advance.

EDIT

This is the as of the main swf

    package 
{
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.net.URLRequest;
    import flash.text.TextField;

    /**
     * ...
     * @author chchrist
     */
    public class CardSender extends Sprite 
    {

        private var loader:Loader;
        private var viewButtonArray:Array;
        private var sendButtonArray:Array;
        private var cardContainer:Sprite;

        public var testVar:String

        public function CardSender() {

            testVar = "it worked!!!";
            init();
        }

        private function init() {
            viewButtonArray = [cardsenderMC.view01, cardsenderMC.view02, cardsenderMC.view03, cardsenderMC.view04];

            for each( var viewButton in viewButtonArray) {
                viewButton.buttonMode = true;
                viewButton.addEventListener(MouseEvent.CLICK, onViewClick);
            }
        }

        private function onViewClick(e:MouseEvent):void {
            var cardName = "card"+e.target.name.substring(4)+".swf";
            loader = new Loader();
            loader.load( new URLRequest(cardName));
            loader.contentLoaderInfo.addEventListener(Event.INIT, onCardInit);
        }

        private function onCardInit(e:Event):void {
            addChild(loader);
            var content:* = loader.content;
            content.closeButton.addEventListener(MouseEvent.CLICK, onCloseClick);
        }

        private function onCloseClick(e:MouseEvent):void {
            removeChild(loader);
        }

    }
}

and I am trying to access the test variable from the loaded swf with this code

trace(MovieClip(parent.parent).testVar);

EDIT#2

If I addChild(loader) not in the onCardInit but in the onViewClick and in the first frame of the loaded swf i have Object(parent.parent).testVar it works...but why it doesn't want to be inside the onCardInit function?

Answer

Amarghosh picture Amarghosh · Nov 25, 2009

You have just one loader variable and you're creating four Loader instances and assigning them to it - so if you are clicking all four of them in quick succession, the loader variable will effectively contain the Loader object corresponding to the last click. All your addChild calls will act on that same Loader object - the others will not be added and their parents will continue to be null

Make the following changes to the code:

private function onCardInit(e:Event):void 
{
    addChild(LoaderInfo(e.currentTarget).loader);
    var content:DisplayObject = loader.content;
    trace(content);
    trace(content.closeButton);
    content.closeButton.addEventListener(MouseEvent.CLICK, onCloseClick);
}

private function onCloseClick(e:MouseEvent):void 
{
    trace(e.currentTarget);//should be the closeButton
    trace(e.currentTarget.parent);//should be the loaded swf
    trace(e.currentTarget.parent.parent);//should be the Loader object that loaded it

    //The following will work only if last three traced correctly
    removeChild(e.currentTarget.parent.parent);
}

Answer to the following update in the question:

If I addChild(loader) in the onViewClick instead of onCardInit, then Object(parent.parent).testVar in the first frame of the loaded swf works. Why doesn't it work if I addChild inside the onCardInit function?

Again, that might be because you are adding a different loader object - are you clicking on more than one viewbuttons? Click on a view-button and wait till it is loaded to see it working.