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...
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...