如何访问不是 DisplayObject 的类中的 stage?
如何在不是我的主类且不是显示对象的类中访问 Actionscript 3 中的舞台?
How do I access the stage in Actionscript 3 in a class which is not my main class and not a displayobject?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最简单的方法是,您可以将其保存在静态变量中,例如:
在另一个类中导入保存静态变量的类,当然在访问它之前必须初始化该变量。
The easy way, you can keep it in a
static var
for example:and in your other class import the class that is holding the static var, of course the var have to be initialized before accessing it.
最简单的方法是使用全局对象
http://github.com/inruntime/AS3-Global -Object
此页面提供了如何从任何类设置和检索对象的示例。
the easiest way is to use a global object
http://github.com/inruntime/AS3-Global-Object
this page has examples of how to set and retrieve objects from any class.
Adobe 未能提供对舞台的静态访问,因此您别无选择,只能自己实现。
这是一个史诗般的失败,因为在主文档类实例构造函数运行以将阶段实例存储在某个任意静态变量中之前不可能访问阶段。
由于每次需要静态访问阶段时都必须初始化该任意静态变量,因此最好确保只需执行一次。
为了确保您只需初始化它一次,您必须确保您编写的每个静态方法都指向该变量,并且在初始化之前不会尝试访问它。
鉴于这一切......最合乎逻辑的步骤是:
1. 立即向 Adobe 提出功能请求。
2. 创建一个“全局”或“文档”基类,为您初始化静态阶段变量,并让所有文档类继承它。扩展 MovieClip 为您提供了最大的灵活性:
您不必多次编写此类,只要所有文档类都扩展上面定义的“Document”类(并立即在其构造函数中调用“super”)。通过这样做,您的文档的构造函数代码以及从该点开始的项目的其余部分将可以通过“Document.sstage”静态访问舞台。在主文档类中发生此初始化之前,静态上下文无法访问阶段。
我建议您尽早采用这种一致性,因为如果 Adobe 添加对 Stage 的静态访问,修复起来会更容易。只需将“Document.sstage”替换为 Adobe 提供的任何内容即可。
Adobe failed to provide static access to the stage, leaving you no option but to implement it yourself.
This is an epic fail, since it's impossible to access the stage before your main document class instance constructor runs to stash the stage instance in some arbitrary static variable.
Since you'll have to initialize that arbitrary static variable every time you want static access to the stage, it's best to ensure you only have to do it once.
To ensure you'll only have to initialize it once, you'll have to make sure that EVERY STATIC METHOD you ever write points to that variable and doesn't try to access it before it's initialized.
Given all that... the most logical steps are:
1. File a Feature Request with Adobe NOW.
2. Create a "Global" or "Document" base class that initializes a static stage variable for you, and have all your document classes inherit from it. Extending MovieClip gives you the most flexibility:
You will not have to write this class more than once, as long as all your document classes extend the above defined "Document" class (and calls "super" in its constructor right away). By doing that, your document's constructor code and the rest of your project from that point forward will have static access to the stage via "Document.sstage". There is no way for a static context to have access to the stage before this initialization occurs in the main document class.
I suggest you adopt this kind of consistency very early on, because it will make it easier to fix if Adobe ever adds static access to Stage. It will simply be a matter of replacing "Document.sstage" with whatever Adobe provides.
您可以使用访问器和修改器类来设置和检索舞台实例吗?
You could use accessor and mutator class to set and retrieve the stage instance?