AS3 从任何地方访问类实例

发布于 2024-12-23 11:33:19 字数 306 浏览 1 评论 0原文

对于我当前的项目,我开始使用 AS3,并且编写了一个 ClipManager 类,在初始化期间我可以在其中定义像“mainView”这样的 MC,如下所示:

clipManager:ClipManager = new ClipManager(mainView);

使用我的 ClipManager,我现在可以轻松地将内容加载到 mainView 等中。问题是我希望整个过程中的每个按钮都可以访问该实例的类方法来更改 mainView。我可以在 Flash 中拥有类似全局类实例的东西吗?或者有没有更智能的方法来实现我想要做的事情?

for my current project I am starting to work with AS3 and I have written a ClipManager class where I can define an MC like "mainView" during initialization like this:

clipManager:ClipManager = new ClipManager(mainView);

With my clipManager I can now easily load stuff into the mainView etc. The problem is that I want every button throughout the whole thing to access Class Methods of this instance to alter the mainView. Can I have something like a global Class instance in Flash or is there any smarter way to achieve what I am trying to do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

沫尐诺 2024-12-30 11:33:19

您可以将 ClipManager 类添加为静态某个地方 - 即上帝对象 - (可能是您的主类)并通过它访问它,或者您可以使用 单例 模式。

在 as3 中实现它的常见方法:

public class Singleton
{
    private static m_instance:Singleton = null; // the only instance of this class
    private static m_creating:Boolean   = false;// are we creating the singleton?

    /**
     * Returns the only Singleton instance
     */
    public static function get instance():Singleton
    {
        if( Singleton.m_instance == null )
        {
            Singleton.m_creating    = true;
            Singleton.m_instance    = new Singleton;
            Singleton.m_creating    = false;
        }
        return Singleton.m_instance;
    }

    /**
     * Creates a new Singleton. Don't call this directly - use the 'instance' property
     */
    public function Singleton()
    {
        if( !Singleton.m_creating )
            throw new Error( "The Singleton class can't be created directly - use the static 'instance' property instead" );
    }
}

现在,要访问您的类,您可以调用 Singleton.instance。该类永远只有一个实例。

至于反模式等,那是另一篇文章了:)

You can either add your ClipManager class as a static somewhere - i.e. a god object - (perhaps your main class) and access it through that, or you can use the Singleton pattern.

A common way to implement it in as3:

public class Singleton
{
    private static m_instance:Singleton = null; // the only instance of this class
    private static m_creating:Boolean   = false;// are we creating the singleton?

    /**
     * Returns the only Singleton instance
     */
    public static function get instance():Singleton
    {
        if( Singleton.m_instance == null )
        {
            Singleton.m_creating    = true;
            Singleton.m_instance    = new Singleton;
            Singleton.m_creating    = false;
        }
        return Singleton.m_instance;
    }

    /**
     * Creates a new Singleton. Don't call this directly - use the 'instance' property
     */
    public function Singleton()
    {
        if( !Singleton.m_creating )
            throw new Error( "The Singleton class can't be created directly - use the static 'instance' property instead" );
    }
}

Now, to access your class, you call Singleton.instance. There'll only ever be one instance of this class.

As for anti-patterns etc, well that's another post :)

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