PDA

View Full Version : Navigation button


mitee
07-09-2009, 02:26 AM
Hi everyone, I have done 2 projects so far with AS3 and its been hell. I come form a AS1/AS2 background mostly (no expert by far). Below is a block of code that will have comments on them with "There must be a better way". It is working but I need to streamline this code for my sanity and learn more because I feel that I am doing it wrong.

The code below has 3 movie clips with 2 frames labeled "normal" and "over". When the user clicks one, it disables itself goes to the "over" state. When the user clicks on any other 2, the disabled button activates itself the the new one disables. Pretty straight forward sticky navigation.


import flash.events.*;
import flash.display.*;
//----

button1.gotoAndStop("over");
button1.enabled=false;

var lastButton=null;
var curButton=button1;

//There must be a better way to declare this other than a loop.
button1.mouseChildren=false;
button1.useHandCursor=true;
button1.buttonMode=true;
button2.mouseChildren=false;
button2.useHandCursor=true;
button2.buttonMode=true;
button3.mouseChildren=false;
button3.useHandCursor=true;
button3.buttonMode=true;

//----
button1.addEventListener(MouseEvent.MOUSE_OVER, goOver,false,0,true);
button1.addEventListener(MouseEvent.MOUSE_OUT, goOut,false,0,true);
button1.addEventListener(MouseEvent.CLICK, onClickIt,false,0,true);

button2.addEventListener(MouseEvent.MOUSE_OVER, goOver,false,0,true);
button2.addEventListener(MouseEvent.MOUSE_OUT, goOut,false,0,true);
button2.addEventListener(MouseEvent.CLICK, onClickIt,false,0,true);

button3.addEventListener(MouseEvent.MOUSE_OVER, goOver,false,0,true);
button3.addEventListener(MouseEvent.MOUSE_OUT, goOut,false,0,true);
button3.addEventListener(MouseEvent.CLICK, onClickIt,false,0,true);
//----
function goOver(event:MouseEvent):void {
event.target.gotoAndStop("over");
}

function goOut(event:MouseEvent):void {
event.target.gotoAndStop("normal");
}

function onClickIt(event:MouseEvent):void {
lastButton=curButton;
curButton.gotoAndStop("normal")
curButton=event.target;

//There must be a better way to REMOVE ALL event listeners form a MC
curButton.removeEventListener(MouseEvent.MOUSE_OVE R, goOver);
curButton.removeEventListener(MouseEvent.MOUSE_OUT , goOut);
curButton.removeEventListener(MouseEvent.CLICK, onClickIt);

//There must be a better way to ADD ALL event listeners form a MC
lastButton.addEventListener(MouseEvent.MOUSE_OVER, goOver);
lastButton.addEventListener(MouseEvent.MOUSE_OUT, goOut);
lastButton.addEventListener(MouseEvent.CLICK, onClickIt);

event.target.gotoAndStop("over");
event.target.enabled=false
;
}



Ok, now that I have this "generic" sticky nav, what if I want to execute something when a specific button is pressed? Do I have to make a bunch of "If" statements inside the click function?

If (event.target=button1){
blah blah blah
}

Thanks for your time in advance...

finaiized
07-09-2009, 02:51 AM
If you don't feel like writing them all out, use a for loop for both. Event listener for loop: http://blog.reyco1.com/method-of-removing-all-event-listeners/ Button initializing for loop would look like this:for (i:int=0; i<3; i++)
{
button[i].mouseChildren=false;
button[i].useHandCursor=true;

}
Be warned though, for loops stops Flash in it's track until it is complete. A loop like this one would probably be un-noticeable. However, it's something you might want to look out for.

mitee
07-09-2009, 06:21 PM
Ok I can live with a loop. Is there a way to turn on and off all listeners on a MC or so I have to do one at a time?

jeremy1987
07-10-2009, 07:52 PM
for (i:int=0; i<3; i++) {
button[i].mouseChildren=false;
button[i].useHandCursor=true;
button[i].addEventListener(MouseEvent.MOUSE_OVER, goOver,false,0,true);
}

mitee
07-10-2009, 07:58 PM
Thanks that makes sense. Hopefully I can figure out a way to optimize the rest of the code.