Actionscript 3 访问主类阶段事件侦听器
我想从主类阶段删除一个事件侦听器,但出现了错误Actionscript 3 访问主类阶段事件侦听器,actionscript-3,Actionscript 3,我想从主类阶段删除一个事件侦听器,但出现了错误1120:Access of undefined property阶段。如何实际访问该阶段 自定义类: import main; main.disableVcam(); 主要类别: public static function disableVcam():void { trace("disable"); stage.removeEventListener(MouseEvent.MOUSE_MOVE,
1120:Access of undefined property阶段。
如何实际访问该阶段
自定义类:
import main;
main.disableVcam();
主要类别:
public static function disableVcam():void {
trace("disable");
stage.removeEventListener(MouseEvent.MOUSE_MOVE, movevC);
}
除非对象位于显示阶段,否则
阶段
对象将未定义(或为空)。您必须addChild
对象才能使stage
对象具有值
编辑:也许您可以在事件处理程序中处理这个问题
protected function clickHandler(e :Event) :void {
if (e.target.stage) {
e.target.stage.removeEventListener(...);
}
}
Edit2:静态方法没有阶段,所以要解决您的问题,您可以将主类设置为单例,并按如下方式工作:
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);
}
如果您的主类是项目的主类,则需要在构造函数中分配静态
实例
变量的值。我使用Event.added\u to\u stage将这些类添加到stage,但只有当按下另一个动态类的按钮时才会调用该类,这与其他几个类的层类似。我只能获取trace语句,但无法获取MouseeEvent工作。也许您可以尝试在事件处理程序中使用event.target.stage
。啊,对不起,我的问题。我现在才注意到这个神奇的词,在本例中是static
。只有类(=对象)的实例才会有一个阶段
,因为无法将静态类添加到显示列表中。添加了一个可能解决方案的示例。