2011-10-19 10 views
0

私はas3に2つのswf(a.swf anb b.swf)を持っています。今私は渡し変数をa.swfからb.Swf.iへこのリンクに従いますTutorial 私はローダーClass.Here私のコーディングを使用しました。変数を渡す方法ActionScript3のSwfから別の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()

どのように私はどのように私は1つのSWFから別のSWFに文字列を渡すことはできますか?この問題を修正することはできますを得ましたか。 誰でも親切に説明してください。 事前に進入する

+0

SWFはどちらも単一のSWFですか? – Benny

+0

いいえ、先生はどちらも違います – user990947

答えて

0

ExternalInterfaceオブジェクトを使用して、SWF間の通信を改善できます。

hereを参照してください。

次のリンクをご覧ください。

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

プラスチックチョウザメによって:

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"); トレース(loader.content ['value']); } loader = new Loader(); loader.contentLoaderInfo.addEventListener(Event.COMPLETE、onLoaded); loader.load(新しいURLRequest( "a.swf")); – user990947

+0

しかしnull値を返します – user990947

+0

エラーメッセージがありますまた、TypeError:エラー#1009:nullオブジェクト参照のプロパティまたはメソッドにアクセスできません。 \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")); 

をチェックしてください、あなたはそれ
更新のためのローカルサーバーが必要になります:私は、これは古いトピックです知っているが、私は最近smartfoxを参照しなければならなかった

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

同じエラーが発生しましたsir.Kindly help me.iローカルサーバーに接続しました – user990947

0

サーバーインスタンスと複数のswfs間の変数。私はこれをどのように達成したかについての基本を分かち合うと思った。 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:*)を介して、子クラスに親クラスを渡すことによって達成することができます見ることができるように機能。

関連する問題