2010-12-11 171 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();