views:

84

answers:

1

Hi:

I have a movie with a document class (Main.as) wich load 2 SWF:

private var mainContainer:Sprite = new Sprite();
addChild(mainContainer);

var loaderx:Loader = new Loader();
loaderx.contentLoaderInfo.addEventListener(Event.COMPLETE,loadingComplete);

loaderx.load(new URLRequest("PhotoLoader.swf")); // PhotoLoader.as


var viewer:Loader = new Loader();
viewer.contentLoaderInfo.addEventListener(Event.COMPLETE,loadingComplete);

viewer.load(new URLRequest("PhotoViewer.swf")); // PhotoViewer.as

private function loadingComplete(evt:Event):void {
  evt.target.removeEventListener(Event.COMPLETE,loadingComplete);
  mainContainer.addChild(evt.target.content);
}

Now I need to access some var/objects in PhotoLoader from PhotoViewer but anytime I compile PhotoViewer the compiler complains:

trace(root.loaderx.dbFields);
1119: Access of possibly undefined property loaderx through a reference with \
static type flash.display:DisplayObject.

Notice I need communication between the 2 loaded SWFs, not from the movie that loaded them

A: 

EDIT

I'm surprised that type casting as a MovieClip fails but then again I only have the code you're showing here to go by. According to the error message you can do this then :

var photoLoader:PhotoLoader = PhotoLoader(evt.target.content );

or even

evt.target.content as PhotoLoader

which is even better than casting as a MovieClip since you can directly access the properties & methods of the PhotoLoader class!

I'm not sure what your environment is and you're right, I assumed that you wanted to access the classes from the the Main class. Clearly my mistake! If you have DocumentClasses on each SWF , then it's done to the way your code is structured, you don't event need LocalConnection, you could have a Singleton Class to be used as a FrontController, a central access to the properties of both classes.

END OF EDIT

First you could cast your loader content as MovieClip, like this:

var content:MovieClip = MovieClip(evt.target.content);
mainContainer.addChild(content);

evt.target.content should be from DisplayObject type , therefore you can't access your movie clips properties.

If you need to access the properties of your loaded SWF, you should declare them like this:

private var photoViewer:MovieClip;
private var photoLoader:MovieClip;

then you could do:

photoViewer = MovieClip(evt.target.content);
mainContainer.addChild(photoViewer);

photoLoader = MovieClip(evt.target.content);
mainContainer.addChild(photoViewer);

just give a name to your loader to differentiate the MovieClips

loaderx.name = "photoLoader";
viewer.name = "photoViewer";

then you can do this

private function loadingComplete(evt:Event):void 
{
    switch(event.currentTarget.loader.name)
   {
        case "photoLoader":
          photoLoader = MovieClip(evt.target.content);
          mainContainer.addChild(photoLoader);
          break;

        case "photoViewer":
          photoViewer = MovieClip(evt.target.content);
          mainContainer.addChild(photoViewer);
          break;
   }

  if( photoViewer!= null && photoLoader!= null )
       evt.target.removeEventListener(Event.COMPLETE,loadingComplete);
}

After that you should be able to access your MovieClips properties like this

trace( photoLoader.dbFields );

or

var prop:Object = photoViewer.whateverNameYouGaveToYourProperty;
PatrickS
Corret me if I'm wrong, I guess you're accesing PhotoLoader data from the Main movie wich loaded that SWF. What I need is access data in PhotoLoader from PhotoViewer, the other SWF loaded in MainI can't see a simple way to have a reference in PhotoViewer pointing to PhotoLoader, to avoid the logic compiler error. So, I think I'll move to LocalConnection classAnyway, I tested your code and received this error:Error #1034: Type Coercion failed: cannot convert classes::PhotoLoader@19a39041 to flash.display.MovieClip.line 44: photoLoader = MovieClip(evt.target.content);
hsands
Thanks a lot for your time, you've been very helpful. Regarding the Singleton Class, I'm going to post a new question as the subject transcend this thread
hsands
You're welcome! If you consider the question answered , please mark it as answered for other people to know. The Singleton class is not the subject here , you're right, but the subject is how you structure your code so that information is accessible from one SWF to another , this has more to do with a simple MVC pattern where you use a controller class to fetch and dispatch info throughout your application, that controller being a singleton.
PatrickS