views:

3451

answers:

4

Hello,

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?

A: 

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

Oliver
A: 

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
Yes i agree but my real problem now is how to access from the loaded swf the testVar variable.
chchrist
Where are you trying to access it? from the constructor of document class? event handler? button class? - I think some code would help
Amarghosh
from the first frame of the loaded swf
chchrist
updated [15 chars]
Amarghosh
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
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
A: 

I am having troubles with the same shit. I also have a movieclip inside a loaded swf, which has to load another external swf. So not a movieclip in the parent.swf , that works, but a movieclip in the initial loaded intro.swf (loaded in the parent.swf).

TypeError: Error #1034: Type Coercion failed: cannot convert com.wonderfl::ParentScene@26fb5481 to flash.display.MovieClip. blabla.

The eventlistener for the movieclip is: boei.wegwijzer.addEventListener (MouseEvent.CLICK, klik); function klik (event:MouseEvent):void { if (MovieClip(parent.parent.parent) != null) { MovieClip(parent.parent.parent).movieInladen ("beach"); } trace("boei luistert"); }

Which is inside a separate class in an .as file. the trace works but I have no idea what I am doing :).... somebody an idea why the parent.swf can't unload the intro.swf and load the beachmovie?

Mike
A: 

Hi ChChrist, sorry that I didn't put an answer, but did you solve your problem last month? Was it a loader-object - movieclip interchange problem? thnx

Mike