How to pass arguments to constructor if object is loaded dynamically with Loader in flash?
Is this possible ? If yes how ?
Otherwise what's the alternatives ?
By dynamically I mean using
loader = new Loader();
loader.contentLoaderInfo.addEventListener(E开发者_JAVA百科vent.COMPLETE, finishLoading);
loader.load(new URLRequest("MySWF.swf"));
A SWF is not a class by itself, more like a collection of classes and other things (like images or audio bytes) all archived and ready to use. You can't have a constructor for a SWF. However, what you can do, is loading a SWF and then, after the loading is complete, you can instantiate a class from that SWF and pass whatever arguments you want to it's constructor.
Also, it's possible to send parameters to the SWF and then process them as flashvars inside the swf, but that's no constructor of course :)
loader.load(new URLRequest("MySWF.swf?day=tue&week=3"));
And then you can get them like this:
var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters;
trace(paramObj.day);
trace(paramObj.week);
If you have a document class in your SWF, then rather than using parameters in the constructor create a public init method with your parameters, or even use getters and setters:
import flash.display.MovieClip;
public class MySWF extends MovieClip
{
private var _var1 :String;
private var _var2 :int;
public function MySWF():void
{
}
public function init(var1:String):void
{
_var1 = var1;
}
public function get var2():int
{
return _var2;
}
public function set var2(value:int):void
{
_var2 = value;
}
}
Then you can call these after you have loaded your swf like this:
private function finishLoading(event:Event):void
{
var mySWF:MySWF = event.target.content as MySWF;
addChild(mySWF);
mySWF.init("This is a string");
mySWF.var2 = 5;
{
精彩评论