可能滥用evt.target.name。 "错误#1010:术语未定义且没有属性。"

时间:2014-05-25 09:08:16

标签: actionscript-3 flash

到目前为止,这个程序应该在父对象被鼠标悬停时使子对象可见,但我似乎无法让它工作。我在这里有点过头了。这是我的代码:

var iconCompArray:Array = new Array();

stage.addEventListener(MouseEvent.MOUSE_MOVE, moveMouse);
addIcons();

function addIcons():void
{
    var iconComp:IconComp = new IconComp();
    iconComp.x = stage.stageWidth/4;
    iconComp.y = stage.stageHeight/4;
    iconComp.iconImage.gotoAndStop(2);
    iconComp.iconHighlight.visible = false;
    iconComp.iconTitle.text = "Program X";
    iconCompArray.push(iconComp);
    addChild(iconComp);
}

function moveMouse(evt:MouseEvent):void
{
    for (var i:int = 0; i < iconCompArray.length; i++)
    {
        if (evt.target.name == iconCompArray[i])
        {
            iconCompArray[i].iconHighlight.visible = true;
        }
    }
}

一个新的&#34; iconComp&#34;从库中拉出并通过iconCompArray添加到舞台中。我们的想法是,当您将鼠标悬停在图标上时,它周围会出现一个蓝色框(iconHighlight.visible)。但出于某种原因,我在这里所做的并不奏效。我想我可能错误地使用了evt.target.name但我无法找到解决方案。这是输出中出现的错误消息:

TypeError: Error #1010: A term is undefined and has no properties.
    at as3_fla::MainTimeline/addIcons()
    at as3_fla::MainTimeline/frame1()

1 个答案:

答案 0 :(得分:0)

从错误中你得到的问题似乎是在addIcons函数中。您应该检查所有影片剪辑的孩子的名称是否正确。影片剪辑的属性mouseChildren应设置为false。此外,如果您使用MOUSE_OVER和MOUSE_OUT事件而不是MOUSE_MOVE可能会更好。

function addIcons():void
{
  var iconComp:IconComp = new IconComp();
  iconComp.x = stage.stageWidth / 4;
  iconComp.y = stage.stageHeight / 4;
  iconComp.iconImage.gotoAndStop(2);
  iconComp.iconHighlight.visible = false;
  iconComp.iconTitle.text = "Program X";
  iconComp.mouseChildren = false;

  iconComp.addEventListener(MouseEvent.MOUSE_OVER, onIconOver);
  iconComp.addEventListener(MouseEvent.MOUSE_OUT, onIconOut);

  iconCompArray.push(iconComp);
  addChild(iconComp);
}

事件监听器应如下所示:

function onIconOver(evt:MouseEvent):void {
  IconComp(evt.target).iconHighlight.visible = true;
}

function onIconOut(evt:MouseEvent):void {
  IconComp(evt.target).iconHighlight.visible = false;
}

如果您在调试模式下发布swf(Ctrl + Shift + Enter),您还可以看到给出错误的代码行。