简体   繁体   English

as3加载的swf访问变量

[英]as3 loaded swf accessing variables

I have some questions with sharing/using/accessing variables/functions between loaded swf files. 我在加载的swf文件之间共享/使用/访问变量/函数时遇到一些问题。 my prj consists of main.swf file and 2 swf's which I load on first init of the main.swf. 我的prj由main.swf文件和2个swf文件组成,我在main.swf的第一个init中加载该文件。 my questions are: 1.how can I use variables from 1.swf in 2.swf (function is running in 2.swf) 2.how can I call a function from 2.swf in 1.swf 我的问题是:1.如何在2.swf中使用1.swf中的变量(函数在2.swf中运行)2.如何在1.swf中从2.swf中调用函数

here is the code I'm using to load the swf's: 这是我用来加载swf的代码:

var playerMc:MovieClip = new MovieClip();
var dbMc:MovieClip = new MovieClip();
var m2Loader:Loader = new Loader();
var mLoader:Loader = new Loader();

startLoad();
function startLoad()
{
    //var mLoader:Loader = new Loader();
    var mRequest:URLRequest = new URLRequest("./_player/player.swf");
    mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadMc);
    mLoader.load(mRequest);
    addChild(mLoader);

    //var m2Loader:Loader = new Loader();
    var m2Request = new URLRequest("./_db/db.swf");
    m2Loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadMc2);
    m2Loader.load(m2Request);
    addChild(m2Loader);
}
function loadMc(event:Event):void
{
    if (! event.target)
    {
        return;
    }
    playerMc = event.target.content as MovieClip;
    mLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadMc);
}

function loadMc2(event:Event):void
{
    if (! event.target)
    {
        return;
    }
    dbMc = event.target.content as MovieClip;
    dbMc.x = -400;
    m2Loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadMc2);
}

You have to stick with application domain. 您必须坚持应用程序域。 In most cases you should load another swf in another application domain, but it's not really related to your question. 在大多数情况下,您应该在另一个应用程序域中加载另一个SWF文件,但这与您的问题并没有真正的关系。 From loader , you must access to applicationDomain and then getDefinition . loader ,您必须访问applicationDomain ,然后访问getDefinition From there, you can get classes and use them in your main swf. 从那里,您可以获取类并在主swf中使用它们。 Yes, you can read static properties. 是的,您可以读取静态属性。 If you need instances you should access loader#content. 如果需要实例,则应访问loader#content。 It is pointing to a root of loaded SWF. 它指向已加载的SWF的根。 Root of loaded is SWF – is the instance of main class of the loaded swf. 加载的根是SWF –是加载的swf的主类的实例。

Create a variable with no definition such as 创建一个没有定义的变量,例如

public var MyClass; 公共var MyClass;

as you can see i didnt add 如您所见,我没有添加

public var MyClass:Class; 公共var MyClass:Class;

then in another function write 然后在另一个函数中写

this.MyClass = this.mLoader.contentLoaderInfo.applicationDomain.getDefinition("NameOfClass") as Class; this.MyClass = this.mLoader.contentLoaderInfo.applicationDomain.getDefinition(“ NameOfClass”)作为类;

i dont know much about this myself.. im having problems figuring out if you can only access Public static variables or if its possible to access normal public variables and possibly private variables because it is creating a new instance of the same class or however you want to word it..? 我本人对此并不了解。.我在弄清楚您是否只能访问Public静态变量,或者是否有可能访问普通的Public变量以及私有变量的问题时遇到了问题,因为它正在创建相同类的新实例,或者您想要措辞..?

also after your write the above code .. when you want to change a varaibles this usually works for me 同样在您编写完上面的代码后..当您想更改变量时,这通常对我有用

this.MyClass.RandomVariableName = this.MyClass.RandomVariableName + 1; this.MyClass.RandomVariableName = this.MyClass.RandomVariableName +1;

something like that.. 像这样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM