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