如果您打算在離開你的代碼在時間軸上,和你的聽衆只需要在運行時進行設置,那麼你並不真的需要包裝聽者實例化的功能,你現在擁有它。剛帶他們出去的功能,並把它們的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;
}
}
如果您將trace(e.currentTarget)作爲onActionPerformed()方法的第一行輸出您期望的內容嗎? – greggreg 2010-12-11 16:35:17
不,但是我從來沒有用Flash做過輸出,所以我應該期待谷歌在電影窗口中彈出,或打開我的默認瀏覽器?無論哪種方式,它既不。 – 2010-12-11 16:38:12
以及如果在單擊按鈕時沒有生成任何輸出,那麼問題出現在方法調用之前。你有沒有嘗試追蹤陳述?它在閃光燈輸出到輸出面板。在你的應用程序的開始嘗試編碼:trace(「我對調試很有用」) – greggreg 2010-12-11 16:47:28