2011-10-19 62 views
0

我在as3中有兩個swf(a.swf anb b.swf)。現在我想要傳遞變量從a.swf到b.Swf.i按照這個鏈接Tutorial 我使用了Loader Class.Here我的編碼。如何傳遞變量從一個Swf到ActionScript3中的另一個Swf?

var loader:Loader = new Loader(); 
loader.load("a.swf?test=hi"); 

,但我得到錯誤信息

TypeError: Error #1034: Type Coercion failed: cannot convert "textchat.swf?test=hi" to flash.net.URLRequest. at BasicTickerSprite_fla::MainTimeline/frame1()

我怎樣才能糾正這個問題?我怎麼能傳遞一個字符串從一個SWF文件到另一個SWF? 任何人都可以詳細解釋我 提前Thaks

+0

這兩個SWF都在單個SWF中嗎? – Benny

+0

不,先生,兩者都不同 – user990947

回答

0

您可以使用ExternalInterface對象在SWF之間實現更好的通信。

請參閱here以獲取Adobe參考。

並查看以下鏈接。

1

ExternalInterface的是一個好方法。

也許更好,但會LocalConnection類:

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/LocalConnection.html

也許一個良好的開端,瞭解基本的概念是從堆棧溢出此處提供這樣的回答:

How to pass a String from a SWF into another SWF

回答by Plastic Sturgeon:

Let's clarify a bit: 1. The loader swf, we will call the parent. 2. The swf loaded by the parent we will call the child.

The Child contains a string, and you want the parent to be able to read that string> So... The Child must define a public variable for the string. (This means you have to use a Class file for it, since you cannot declare a property public on the timeline.)

Finally, the parent will try and get that property. You may want to wrap that in a try/catch to handle cases where the string will not be present.

Here is an example Child Class.

package 
{ 
import flash.display.Sprite; 

/** 
* ... 
* @author Zach Foley 
*/ 
public class Child extends Sprite 
{ 
    public var value:String = "This is the child Value"; 
    public function Child() 
    { 
     trace("Child Loaded"); 
    } 

} 
} 

And here is the parent loader class:

package 
{ 
import flash.display.Loader; 
import flash.display.Sprite; 
import flash.events.Event; 
import flash.net.URLRequest; 

/** 
* ... 
* @author Zach Foley 
*/ 
public class Parent extends Sprite 
{ 
    private var loader:Loader; 

    public function Parent() 
    { 
     trace("PArent Init"); 
     loader = new Loader(); 
     loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded); 
     loader.load(new URLRequest("child.swf")); 
    } 

    private function onLoaded(e:Event):void 
    { 
     trace("Child Loaded"); 
     trace(loader.content['value']); 
    } 

} 
} 

The Output will be: PArent Init Child Loaded Child Loaded This is the child Value

+1

+1感謝你的回覆,這對我來說非常有用。我在a.swf中聲明瞭一個變量var value:String =「這是子值」;現在我在B.swf中調用這個值,這裏是我的編碼函數onLoaded(e:Event):void { trace(「Child Loaded」); trace(loader.content ['value']); } loader = new Loader(); loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoaded); loader.load(new URLRequest(「a.swf」)); – user990947

+0

但它返回空值 – user990947

+0

我得到了錯誤消息也TypeError:錯誤#1009:無法訪問空對象引用的屬性或方法。 \t at textchat_fla :: MainTimeline/frame1() – user990947

4
+0

我投了票,因爲這是正確的做法。但是,將來會嘗試提供更多信息,例如簡單示例或指導如何使用它的解釋。 – sberry

1
var loader:Loader = new Loader(); 
loader.load(new URLRequest("http//localhost/a.swf?test=hi")); 

你需要一個本地服務器爲它
更新

private var _ldr:Loader; 
    private var _mc:MovieClip; 
    public function Main():void { 
     if (stage) init(); 
     else addEventListener(Event.ADDED_TO_STAGE, init); 
    } 

    private function init(e:Event = null):void { 
     removeEventListener(Event.ADDED_TO_STAGE, init); 
     // entry point 
     _ldr = new Loader(); 
     _ldr.contentLoaderInfo.addEventListener(Event.INIT, onInit); 
     _ldr.load(new URLRequest('a.swf')); 
    } 

    private function onInit(e:Event):void{ 
     _mc = _ldr.content as MovieClip; 
     _mc['test'] = 'hi'; 
     addChild(_mc); 
    } 
+0

我試過這種編碼,我得到這樣的錯誤,錯誤#2044:未處理的IOErrorEvent :. text =錯誤#2035:URL未找到。 – user990947

+0

對不起,只能通過http,編輯我的答案 – www0z0k

+0

再次發生同樣的錯誤先生。親切地幫助我。我連接我的本地服務器 – user990947

0

我知道這是一個老話題,但最近,我不得不引用smartfox服務器實例和多個swf之間的變量。我想我會分享我如何實現這一目標的基礎知識。

ParentSWF

public var MyVar:String = "Hello"; 
public var sfs:SmartFox = new SmartFox(true); 
private var ChildMC:MovieClip = new MovieClip(); 
private var myLoader:Loader; 

private function LoadChildSWF(evt:Event):void //i used button click event to load swf 
    { 
    myLoader = new Loader();      // create a new instance of the Loader class 
    var url:URLRequest = new URLRequest("YOURSWF.swf"); // in this case both SWFs are in the same folder 
    try 
     { 
      myLoader.load(url); 
      myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,FFLoaded); 

     } 
     catch (error:Error) 
     { 
      trace("Unable to load requested document."); 
     } 
    } 
    function FFLoaded(e:Event):void 
    { 
     ChildMC= myLoader.content as MovieClip; 
     addChild(ChildMC); 
     ChildMC.init(this); 

    } 
    public function RemoveChildMC():void 
    { 
     removeChild(ChildMC); 
     ChildMC=null; 
    } 

ChildSWF

package 
{ 
import com.smartfoxserver.v2.SmartFox; 

public class ChildDocumentClass extends MovieClip 
{ 
    private var refDocument:*; 
    private var ChildVar:String; 
    private var sfs:SmartFox; 
    public function ChildDocumentClass() 
    { 
     trace("Initilise ChildDocumentClass"); 
     // Nothing to do 
    } 
    public function init(refDoc:*):void 
    { 
     // Get the references to the Document Class 
     refDocument = refDoc; 
     // Get the references to the Server Instance 
     sfs=refDocument.sfs; 
    //Pass Parent Variable to child variable 
     ChildVar=refDocument.MyVar; 
    } 
//call to parent class function example to remove loaded swf 
    private function UnloadThisSWF():void 
    { 
     refDocument.RemoveChildMC(); 
    } 
} 
} 

正如你所看到的通信可以通過父類傳遞到子類通過的init(refDoc:*)來實現功能。

相關問題