在时间轴上添加鼠标事件侦听器

时间:2013-06-07 22:33:56

标签: actionscript-3 flash-cs6

我正在尝试向我的按钮添加EventListener,这是另一个MovieClip的孩子,但我收到以下错误。代码放在我的时间线内。

  

TypeError:错误#1009:无法访问null的属性或方法   对象参考。在Exotic_fla :: MainTimeline / frame1()

movClip.Play_btn.addEventListener(MouseEvent.CLICK,playfunc);
//movClip.Play_btn.visible = false;// this give the same result
function playfunc(evt:MouseEvent){
    SoundMixer.stopAll();
    gotoAndPlay(1, "Scene 2");
}

1 个答案:

答案 0 :(得分:0)

如果Play_btn是唯一的孩子,这意味着它是movClip包含的唯一内容,您可以替换

movClip.Play_btn.addEventListener(MouseEvent.CLICK,playfunc);

movClip.getChildAt(0).addEventListener(MouseEvent.CLICK,playfunc);

否则试试这个

for (var i:uint = 0; i < movClip.numChildren; i++) {
            if (movClip.getChildAt(i) is MovieClip) {
                 if (movClip.getChildAt(i).name == "Play_btn") {
                        movClip.Play_btn.addEventListener(MouseEvent.CLICK,playfunc);
                        trace("Play_btn found");
                        break;
                 }
            }
}

function playfunc(evt:MouseEvent) {
    trace("It works");
    SoundMixer.stopAll();
    gotoAndPlay(1, "Scene 2");
}

更新


请确保您已将实例 Play_btnmovClip命名为,并且它不是类名,即库中的名称。 而不是这一行

movClip.Play_btn.addEventListener(MouseEvent.CLICK,playfunc);

使用以下

movClip.getChildByName("Play_btn").addEventListener(MouseEvent.CLICK,playfunc);
相关问题