Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

parent.parent is wrong. try using:

MovieClip(MovieClip(e.currentTarget.parent).parent).myVar ;

You have to cast the first parent to some class like MovieClip and then get this new MovieClip's parent and then getting inside variable.

try tracing parent and parent.parent, I'm sure it's because of an aditional "parent"

loader.content is NOT a movieclip, so it doesn't have the same capabilities. try

myLoadedClip:MovieClip = MovieClip(loader.content);

then your myLoadedClip.parent.myVar should show up

you might want your handler to fire on a Event.COMPLETE instead of on an Event.INIT

This only line code in loaded swf resolves it! ;)

MovieClip(MovieClip(parent.parent).root).play();

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top