5

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?

chchrist
  • 18,854
  • 11
  • 48
  • 82

5 Answers5

2

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.

j0k
  • 22,600
  • 28
  • 79
  • 90
2

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.

Amarghosh
  • 58,710
  • 11
  • 92
  • 121
  • Yes i agree but my real problem now is how to access from the loaded swf the testVar variable. – chchrist Nov 25 '09 at 16:30
  • Where are you trying to access it? from the constructor of document class? event handler? button class? - I think some code would help – Amarghosh Nov 25 '09 at 17:16
  • from the first frame of the loaded swf – chchrist Nov 25 '09 at 18:17
  • I click on a view button which loads a card and takes up all the stage. The only way to remove the card is to hit the close button which is inside the card and call the onCloseClick function. So there is no way that multiple loader objects exist. I want the loaded card to be able to access some varialbes from the main swf which is loaded. – chchrist Nov 26 '09 at 08:36
  • what does `trace(this);` trace - does it work correctly - does it trace name of the document class of the loaded SWF? If it does, there is no way that this.parent will trace Stage - because you are adding it to the loader. The display list would look like - Stage - loader SWF's root - loader object - loaded swf. – Amarghosh Nov 26 '09 at 09:16
0

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

Oliver
  • 487
  • 2
  • 9
0

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

0

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

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