我知道這是一個老話題,但最近,我不得不引用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:*)來實現功能。
這兩個SWF都在單個SWF中嗎? – Benny
不,先生,兩者都不同 – user990947