开发者

AS3 cross-scripting between 2 SWFs problem

开发者 https://www.devze.com 2023-01-12 00:49 出处:网络
I have a movie with a document class (Main.as) wich load 2 SWF: private var mainContainer:Sprite = new Sprite();

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("PhotoLoad开发者_StackOverflow中文版er.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


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消