【问题标题】:Linking buttons not working in actionscript - flash cs4, AS3链接按钮在 actionscript 中不起作用 - flash cs4、AS3
【发布时间】:2011-05-23 23:47:33
【问题描述】:

我在同一层上有 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

【问题讨论】:

  • 如果你把 trace(e.currentTarget) 作为 onActionPerformed() 方法的第一行,它会输出你期望的结果吗?
  • 不,但是我以前从来没有用 flash 输出过,所以我应该期待 google 在电影窗口中弹出,还是打开我的默认浏览器?无论哪种方式都没有。
  • 好吧,如果您单击按钮时它没有生成任何输出,那么您的问题出在方法调用之前。您是否尝试过跟踪语句?它以闪存的形式输出到输出面板。在您的应用程序开始时尝试编码:trace("我对调试很有用")
  • 我在第一个函数之前做了一个,它读到,我在第一个函数的末尾做了一个,它没有读到,然后我在两者之间做了一个,它读到.所以它一定不是在处理init函数?

标签: flash actionscript-3 flash-cs4


【解决方案1】:

如果您打算将代码留在时间线上,并且只需要在运行时设置侦听器,那么您实际上不需要像现在那样将侦听器实例化包装在函数中。只需将它们从函数中取出并将它们放在 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;
    }
}

【讨论】:

    【解决方案2】:

    您必须添加对 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();
    

    【讨论】:

      【解决方案3】:

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

      init();
      

      【讨论】:

        猜你喜欢
        • 2016-10-04
        • 2013-09-30
        • 1970-01-01
        • 2015-04-29
        • 2010-12-01
        • 2013-11-21
        • 2014-08-13
        • 2011-01-02
        • 1970-01-01
        相关资源
        最近更新 更多