当MouseEvent.CLICK包含在按钮中时,错误#1009:onenterFrameHandler

时间:2012-05-26 10:38:34

标签: actionscript-3 flash

使用MovieClip(root)制作幻灯片按钮功能时出现问题。

addEventListener (Event.ENTER_FRAME, onenterFrameHandler);

当给出一个命令按钮.. addEventListener (MouseEvent.CLICK, fl_ClickToGoToScene_20);当我点击按钮时,我得到了输出

  

TypeError:错误#1009:无法访问空对象引用的属性或方法。   在index_fla :: MainTimeline / onenterFrameHandler()

这是我的代码:

var myVar:Boolean;
MovieClip(root).addEventListener(Event.ENTER_FRAME,onenterFrameHandler);
MovieClip(root).addEventListener(MouseEvent.CLICK, mouseClickHandler);

function onenterFrameHandler(event:Event):void
{
if (MovieClip(root).mouseY < 500)
    {
      myVar=false;
    }
if (MovieClip(root).mouseY > 500)
    {
       myVar=true;
    }

if (MovieClip(root).mouseX > 1266 && myVar==true)
{
imgbar.nextFrame();
} else {
    imgbar.play();
}
if (MovieClip(root).mouseX < 100 && myVar==true)
{
imgbar.prevFrame();
} else {
    imgbar.play();
}
    if (MovieClip(root).mouseX > 100 && mouseX < 1266 && myVar==true)
    {
        imgbar.stop();
    }
}

并在按钮层中填写代码以命令我的按钮/ movieclip;

    btn1.addEventListener(MouseEvent.CLICK, fl_ClickToGoToScene);

function fl_ClickToGoToScene(event:MouseEvent):void
{
    MovieClip(this.root).gotoAndPlay(80, "Scene 2");
}

请帮帮我

2 个答案:

答案 0 :(得分:0)

在场景“场景2”中,舞台上不再出现部分或全部MovieClip。因此,ENTER_FRAME处理程序无法再找到imgbar,这会导致错误。要删除它,请包含与上一个场景中的imgbar剪辑(具有相同的舞台名称!),或者如果imgbar不再存在,则删除ENTER_FRAME侦听器:

function onenterFrameHandler(event:Event):void
{
    if (!imgbar) root.removeEventListener(Event.ENTER_FRAME,onenterFrameHandler);
     // the rest stays the same
}

答案 1 :(得分:0)

  

function onenterFrameHandler(event:Event):void
{
    if (!imgbar) root.removeEventListener(Event.ENTER_FRAME,onenterFrameHandler);
     // the rest stays the same
}

是的,我发现它的修改很少,感谢您的建议, weltraumpirat ,这是最终的代码:

    var myVar:Boolean=false;
    MovieClip(root).addEventListener(Event.ENTER_FRAME,onenterFrameHandler);
    MovieClip(root).addEventListener(MouseEvent.CLICK,fl_ClickToGoToAndStopAtFrame_28);


function onenterFrameHandler(event:Event):void
{
if (MovieClip(root).mouseY < 500)
    {
      myVar=false;
    }
if (MovieClip(root).mouseY > 500)
    {
       myVar=true;
    }

if (MovieClip(root).mouseX > 1266 && myVar==true)
{
imgbar.nextFrame();
} else {
    imgbar.play();
}
if (MovieClip(root).mouseX < 100 && myVar==true)
{
imgbar.prevFrame();
} else {
    imgbar.play();
}
    if (MovieClip(root).mouseX > 100 && mouseX < 1266 && myVar==true)
    {
        imgbar.stop();
    }
    if (!imgbar)root.removeEventListener(Event.ENTER_FRAME,onenterFrameHandler);
}

function fl_ClickToGoToAndStopAtFrame_28(event:MouseEvent):void
{
    MovieClip(root).removeEventListener(Event.ENTER_FRAME, onenterFrameHandler);

}