stage.addEventListener包里面?里面、stage、addEventListener

由网友(愿有岁月可回首)分享简介:我试图做这样的事情:package com.clicker{import flash.display.*;import flash.events.MouseEvent;public class Stager extends MovieClip {public function clicker():void {stag...

我试图做这样的事情:

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.

推荐答案

阶段访问只有当你的组件添加到舞台上。如果你想知道的话,你可以使用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");
        }

    }
}
阅读全文

相关推荐

最新文章