链接按钮不在actionscript中工作 - flash cs4,AS3

时间:2010-12-11 16:28:11

标签: flash actionscript-3 flash-cs4

我在同一层上有6个按钮,所有按钮都悬停在效果和排序上。我为每个人分配了一个实例名称,并尝试制作动作脚本将每个图像链接到谷歌,但以下代码不起作用:

function init():void {
    blogButton.addEventListener(MouseEvent.CLICK,onActionPerformed);
    homeButton.addEventListener(MouseEvent.CLICK,onActionPerformed);
    portfolioButton.addEventListener(MouseEvent.CLICK,onActionPerformed);
    aboutButton.addEventListener(MouseEvent.CLICK,onActionPerformed);
    signButton.addEventListener(MouseEvent.CLICK,onActionPerformed);
    contactButton.addEventListener(MouseEvent.CLICK,onActionPerformed);
}

function onActionPerformed(e:MouseEvent):void {
    switch(e.currentTarget) {
       case homeButton: navigateToURL(new URLRequest("http://google.com"), "_blank"); break;
       case blogButton: navigateToURL(new URLRequest("http://google.com"), "_self"); break;
       case portfolioButton: navigateToURL(new URLRequest("http://google.com"), "_self"); break;
       case aboutButton: navigateToURL(new URLRequest("http://google.com"), "_self"); break;
       case signButton: navigateToURL(new URLRequest("http://google.com"), "_self"); break;
       case contactButton: navigateToURL(new URLRequest("http://google.com"), "_self"); break;
    }
}

没有错误或编译错误,只是没有去任何地方。

编辑代码已略有修改,但仍无法正常运行我创建了一个链接来下载最新的fla文件: http://danlamanna.com/misc/navigation.fla

3 个答案:

答案 0 :(得分:1)

您没有运行init函数,因此未设置侦听器。

init();

答案 1 :(得分:0)

您必须添加对init()函数的调用。使用以下内容:

function init():void 
{
        blogButton.addEventListener(MouseEvent.CLICK,onActionPerformed);
        homeButton.addEventListener(MouseEvent.CLICK,onActionPerformed);
        portfolioButton.addEventListener(MouseEvent.CLICK,onActionPerformed);
        aboutButton.addEventListener(MouseEvent.CLICK,onActionPerformed);
        signButton.addEventListener(MouseEvent.CLICK,onActionPerformed);
        contactButton.addEventListener(MouseEvent.CLICK,onActionPerformed);

}// end function

function onActionPerformed(e:MouseEvent):void 
{
 switch(e.currentTarget) 
 {
  case homeButton: navigateToURL(new URLRequest("http://google.com"), "_blank"); break;
  case blogButton: navigateToURL(new URLRequest("http://google.com"), "_self"); break;
  case portfolioButton: navigateToURL(new URLRequest("http://google.com"), "_self"); break;
  case aboutButton: navigateToURL(new URLRequest("http://google.com"), "_self"); break;
  case signButton: navigateToURL(new URLRequest("http://google.com"), "_self"); break;
  case contactButton: navigateToURL(new URLRequest("http://google.com"), "_self"); break;

 }// end switch

}// end function

init();

答案 2 :(得分:0)

如果您计划将代码留在时间轴上,并且只需要在运行时设置侦听器,那么您实际上并不需要将侦听器实例化包装在函数中,就像现在一样。只需将它们从函数中取出并将它们放在onActionPerformed函数之上,如下所示:

blogButton.addEventListener(MouseEvent.CLICK,onActionPerformed);
homeButton.addEventListener(MouseEvent.CLICK,onActionPerformed);
portfolioButton.addEventListener(MouseEvent.CLICK,onActionPerformed);
aboutButton.addEventListener(MouseEvent.CLICK,onActionPerformed);
signButton.addEventListener(MouseEvent.CLICK,onActionPerformed);
contactButton.addEventListener(MouseEvent.CLICK,onActionPerformed);

function onActionPerformed(e:MouseEvent):void 
{
     switch(e.currentTarget) 
     {
         case homeButton: navigateToURL(new URLRequest("http://google.com"), "_blank"); break;
         case blogButton: navigateToURL(new URLRequest("http://google.com"), "_self"); break;
         case portfolioButton: navigateToURL(new URLRequest("http://google.com"), "_self"); break;
         case aboutButton: navigateToURL(new URLRequest("http://google.com"), "_self"); break;
         case signButton: navigateToURL(new URLRequest("http://google.com"), "_self"); break;
         case contactButton: navigateToURL(new URLRequest("http://google.com"), "_self"); break;

     }

}

如果您需要稍后动态添加和删除侦听器,请尝试以下操作:

addListeners();

function addListeners():void
{
    blogButton.addEventListener(MouseEvent.CLICK,onActionPerformed);
    homeButton.addEventListener(MouseEvent.CLICK,onActionPerformed);
    portfolioButton.addEventListener(MouseEvent.CLICK,onActionPerformed);
    aboutButton.addEventListener(MouseEvent.CLICK,onActionPerformed);
    signButton.addEventListener(MouseEvent.CLICK,onActionPerformed);
    contactButton.addEventListener(MouseEvent.CLICK,onActionPerformed);
}

function removeListeners():void
{
    blogButton.removeEventListener(MouseEvent.CLICK,onActionPerformed);
    homeButton.removeEventListener(MouseEvent.CLICK,onActionPerformed);
    portfolioButton.removeEventListener(MouseEvent.CLICK,onActionPerformed);
    aboutButton.removeEventListener(MouseEvent.CLICK,onActionPerformed);
    signButton.removeEventListener(MouseEvent.CLICK,onActionPerformed);
    contactButton.removeEventListener(MouseEvent.CLICK,onActionPerformed);
}

function onActionPerformed(e:MouseEvent):void
{
    switch(e.currentTarget)
    {
        case homeButton: navigateToURL(new URLRequest("http://google.com"), "_blank"); break;
        case blogButton: navigateToURL(new URLRequest("http://google.com"), "_self"); break;
        case portfolioButton: navigateToURL(new URLRequest("http://google.com"), "_self"); break;
        case aboutButton: navigateToURL(new URLRequest("http://google.com"), "_self"); break;
        case signButton: navigateToURL(new URLRequest("http://google.com"), "_self"); break;
        case contactButton: navigateToURL(new URLRequest("http://google.com"), "_self"); break;
    }
}