将var值从一个swf传递给另一个swf,该swf在AS3中的第一个swf中加载

时间:2011-01-02 01:52:37

标签: flash actionscript-3

使用此方法在另一个SWF中加载SWF: (此代码在main.swf中)

function loadImage(url:String):void {
imageLoader = new Loader();
imageLoader.load(new URLRequest(url));
imageLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, imageLoading);
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
}
loadImage("test1.swf");

function imageLoaded(e:Event):void {
MyMovieClip.addChild(imageLoader); 
} 

思考是test1.swf需要一个在加载器swf(main.swf)中的值,我该如何传递该值?谢谢!

2 个答案:

答案 0 :(得分:1)

您可以为加载的SWF(test1.swf)创建一个类,您可以使用Document类分配该类。

在此类中,为需要传递给加载的SWF的值创建一个setter。

   //Here the choice of Array is arbitrary & depends on what values
   //you need to pass to the SWF...
   private var _params:Array;
   public function set params( value:Array ):void
   {
      _params = value;
   }

然后在您的Main课程中,您可以执行以下操作:

  //Assuming you'd like to pass a number of values...
  function imageLoaded(e:Event):void {

      var content:MovieClip = imageLoader.content as CustomClass;
      content.params = [ value1 , value2... valueN];
      MyMovieClip.addChild(content);
  } 

答案 1 :(得分:0)

您需要访问加载程序的内容才能执行此操作。在你的imageLoaded函数中执行此操作。

( e.target.content as Object ).someParam = "whatever";

需要考虑许多安全限制(这称为交叉脚本),但是如果从同一个域加载两个swfs,则应该没有问题。

相关问题