2010-12-11 139 views
0

我有6个按钮在同一层,所有悬停在效果和排序。我分配的每一个实例名称,并设法使动作每个图像链接到谷歌,但下面的代码是不工作:链接按钮不起作用的动作 - 闪光灯CS4,AS3

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

+0

如果您将trace(e.currentTarget)作为onActionPerformed()方法的第一行输出您期望的内容吗? – greggreg 2010-12-11 16:35:17

+0

不,但是我从来没有用Flash做过输出,所以我应该期待谷歌在电影窗口中弹出,或打开我的默认浏览器?无论哪种方式,它既不。 – 2010-12-11 16:38:12

+0

以及如果在单击按钮时没有生成任何输出,那么问题出现在方法调用之前。你有没有尝试追踪陈述?它在闪光灯输出到输出面板。在你的应用程序的开始尝试编码:trace(“我对调试很有用”) – greggreg 2010-12-11 16:47:28

回答

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; 
    } 
} 
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(); 
1

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

init();