包内的 stage.addEventListener? [英] stage.addEventListener inside a package?

查看:36
本文介绍了包内的 stage.addEventListener?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做这样的事情:

I am trying to do something like this:

package com.clicker{
    import flash.display.*;
    import flash.events.MouseEvent;

    public class Stager extends MovieClip {

        public function clicker():void {
            stage.addEventListener(MouseEvent.CLICK, do_stage);
        }
        function do_stage(e:MouseEvent):void {
            trace("stage clicked");
        }

    }
}

但是,我收到了 1009 错误.

But, I get the 1009 error.

当我这样做时:

import com.clicker.*;

var test:Stager = new Stager();
test.clicker();
addChild(test); 

请帮帮我.预先非常感谢您,祝您节日快乐.

Please help me. Thank you very much in advance, and Happy Holidays.

推荐答案

stage 仅在您的组件添加到舞台后才可访问.如果你想知道它,你可以使用 ADDED_TO_STAGE 事件.

stage is accessible only when your component is added to the stage. If you want to know it, you can use the ADDED_TO_STAGE event.

所以,你可以这样做:

package com.clicker{
    import flash.display.*;
    import flash.events.*;

    public class Stager extends MovieClip {

        public function clicker():void {
            addEventListener(Event.ADDED_TO_STAGE, init);
        }
        private function init(e:Event):void {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            stage.addEventListener(MouseEvent.CLICK, do_stage);
        }
        function do_stage(e:MouseEvent):void {
            trace("stage clicked");
        }

    }
}

这篇关于包内的 stage.addEventListener?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆