开发者

Linking buttons not working in actionscript - flash cs4, AS3

开发者 https://www.devze.com 2023-01-30 15:54 出处:网络
I have 6 buttons on the same layer, all with hover over effects and the sort. I assigned each one an instance name, and tried to make actionscript to link each image to google, however the following c

I have 6 buttons on the same layer, all with hover over effects and the sort. I assigned each one an instance name, and tried to make actionscript to link each image to google, however the following code is not working:

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)开发者_StackOverflow: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;
    }
}

No errors, or compiling errors, just doesn't go anywhere.

EDIT The code has been modified slightly, however still not functioning I made a link to download the most current fla file: http://danlamanna.com/misc/navigation.fla


You are not running the function init so the listeners are not being set.

init();


If you are planning on leaving your code on the timeline, and your listeners only need to be set at runtime then you don't really need to wrap the listener instantiation in a function as you have it now. Just take them out of the function and put them above the onActionPerformed function like so:

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;

     }

}

If you need to dynamically add and remove the listeners at later times, try something like this:

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;
    }
}


You must add a call to your init() function. Use the following:

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();
0

精彩评论

暂无评论...
验证码 换一张
取 消