【问题标题】:Accessing main class stage event listener访问主类舞台事件监听器
【发布时间】:2010-04-13 09:10:33
【问题描述】:

我想从主类舞台中删除一个事件监听器,但我收到错误1120: Access of undefined property stage.我如何实际访问舞台?

自定义类:

import main;
main.disableVcam();

主类:

public static function disableVcam():void {
            trace("disable");
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, movevC);
        }

【问题讨论】:

    标签: actionscript-3


    【解决方案1】:

    除非对象在展示台上,否则stage 对象将是未定义的(或为空)。您必须 addChild 对象才能使 stage 对象具有值。

    编辑:也许你可以在事件处理程序中处理这个?

    protected function clickHandler(e :Event) :void {
        if (e.target.stage) {
            e.target.stage.removeEventListener(...);
        }
    }
    

    Edit2:静态方法没有阶段,因此要解决您的问题,您可以将 Main-class 设置为单例,并像这样工作:

    public class Main {
        static private var instance :Main;
    
        static public function getInstance() :Main {
            if (Main.instance == undefined) {
                Main.instance = new Main();
            }
    
            return Main.instance;
        }
    
        // The rest of the class goes here
    }
    
    
    // snip
    
    import Main;
    
    public static function disableVcam():void {
        trace("disable");
        Main.getInstance().stage.removeEventListener(MouseEvent.MOUSE_MOVE, movevC);
    }
    

    如果您的 Main-class 是项目的主类,则需要在构造函数中分配静态 instance 变量的值。

    【讨论】:

    • 我已经使用 Event.ADDED_TO_STAGE 将类添加到舞台,但只有在按下另一个动态类的按钮时才会调用该类,并且它就像其他类的几层一样。我只能得到跟踪语句,但不能让 mouseevent 工作
    • 也许您可以尝试在事件处理程序中使用event.target.stage
    • 啊,对不起,我的错。我现在才注意到这个神奇的词,在这种情况下是static。只有类的实例(= 对象)才会有 stage,因为静态类不能添加到显示列表中。
    • 添加了一个可能的解决方案示例。
    猜你喜欢
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 2010-10-09
    • 1970-01-01
    • 1970-01-01
    • 2016-08-26
    • 1970-01-01
    相关资源
    最近更新 更多