如何访问不是 DisplayObject 的类中的 stage?

发布于 2024-08-19 19:03:34 字数 49 浏览 1 评论 0原文

如何在不是我的主类且不是显示对象的类中访问 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寄离 2024-08-26 19:03:34

最简单的方法是,您可以将其保存在静态变量中,例如:

public class MyMain extends Sprite {
 public static var STAGE:Stage;

 public function MyMain() {
  if (stage)
   init();
  else
   addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
  }
 }

 private function init(e:Event=null):void{
  removeEventListener(Event.ADDED_TO_STAGE, init);
  // store stage reference when stage ready  
  STAGE=stage;
 }
}

在另一个类中导入保存静态变量的类,当然在访问它之前必须初始化该变量。

import MyMain;

public class Other {
 public function useStage():void {
   MyMain.STAGE...
 }
}

The easy way, you can keep it in a static var for example:

public class MyMain extends Sprite {
 public static var STAGE:Stage;

 public function MyMain() {
  if (stage)
   init();
  else
   addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
  }
 }

 private function init(e:Event=null):void{
  removeEventListener(Event.ADDED_TO_STAGE, init);
  // store stage reference when stage ready  
  STAGE=stage;
 }
}

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.

import MyMain;

public class Other {
 public function useStage():void {
   MyMain.STAGE...
 }
}
叹梦 2024-08-26 19:03:34

最简单的方法是使用全局对象

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.

恰似旧人归 2024-08-26 19:03:34

Adobe 未能提供对舞台的静态访问,因此您别无选择,只能自己实现。

这是一个史诗般的失败,因为在主文档类实例构造函数运行以将阶段实例存储在某个任意静态变量中之前不可能访问阶段。

由于每次需要静态访问阶段时都必须初始化该任意静态变量,因此最好确保只需执行一次。

为了确保您只需初始化它一次,您必须确保您编写的每个静态方法都指向该变量,并且在初始化之前不会尝试访问它。

鉴于这一切......最合乎逻辑的步骤是:
1. 立即向 Adob​​e 提出功能请求。
2. 创建一个“全局”或“文档”基类,为您初始化静态阶段变量,并让所有文档类继承它。扩展 MovieClip 为您提供了最大的灵活性:

package
{
    import flash.display.Stage;
    import flash.display.MovieClip;
    import flash.events.Event;
    public class Document extends MovieClip
    {
        public static var _stage:Stage = null;

        public static function get sstage():Stage //added an extra s for "static" to differentiate the static property name from the instance property name "stage"; call it what you want
        {
            return _stage;
        }

        public function Document()
        {
            super();
            if (stage != null)
               initStage( null ); //explicitly pass null to indicate no listener was attached
            else
                addEventListener( Event.ADDED_TO_STAGE, initStage, false, 0, true ); //prefer weak references
        }

        private function initStage( e:Event ):void
        {
            _stage = stage;
            if (e != null) //event listener will be non-null iff listener was added
                removeEventListener( Event.ADDED_TO_STAGE, initStage, false );           
        }
    }
}

您不必多次编写此类,只要所有文档类都扩展上面定义的“Document”类(并立即在其构造函数中调用“super”)。通过这样做,您的文档的构造函数代码以及从该点开始的项目的其余部分将可以通过“Document.sstage”静态访问舞台。在主文档类中发生此初始化之前,静态上下文无法访问阶段。

我建议您尽早采用这种一致性,因为如果 Adob​​e 添加对 Stage 的静态访问,修复起来会更容易。只需将“Document.sstage”替换为 Adob​​e 提供的任何内容即可。

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:

package
{
    import flash.display.Stage;
    import flash.display.MovieClip;
    import flash.events.Event;
    public class Document extends MovieClip
    {
        public static var _stage:Stage = null;

        public static function get sstage():Stage //added an extra s for "static" to differentiate the static property name from the instance property name "stage"; call it what you want
        {
            return _stage;
        }

        public function Document()
        {
            super();
            if (stage != null)
               initStage( null ); //explicitly pass null to indicate no listener was attached
            else
                addEventListener( Event.ADDED_TO_STAGE, initStage, false, 0, true ); //prefer weak references
        }

        private function initStage( e:Event ):void
        {
            _stage = stage;
            if (e != null) //event listener will be non-null iff listener was added
                removeEventListener( Event.ADDED_TO_STAGE, initStage, false );           
        }
    }
}

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.

傲影 2024-08-26 19:03:34

您可以使用访问器和修改器类来设置和检索舞台实例吗?

You could use accessor and mutator class to set and retrieve the stage instance?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文