PDA

View Full Version : adding event listener to array element


varPBR:Tasty
04-10-2008, 03:16 PM
Hello,

I've created a number of buttons and pushed them into an array, then to each element I've added a set of parameters, including a MOUSE_OVER listener. The function called by the MOUSE_OVER listener includes a MOUSE_OUT listener, to call another function, but I can't get it to operate the way I want it to (the do...while is not working)—instead of fading out, the alpha just drops to zero.

I suspect this is because I am referencing the target object, but when the mouse rolls out, there essentially is no longer a target object.

Could anyone look at this and give me a hint as to how I can program an OUT listener for elements pushed into an array such as this?

var buttonArray:Array = [frontB, aboveB, sideB, closeB, homeB, productsB, contactB];

for each(var navButton:MovieClip in buttonArray){
navButton.scaleY = 16;
navButton.buttonMode = true;
navButton.addEventListener(MouseEvent.MOUSE_OVER, buttonHighlight);
navButton.alpha = 0;
addChild(navButton);
}

//navigation button functions
function buttonHighlight(e:MouseEvent):void{
e.target.alpha = .25;
e.target.addEventListener(MouseEvent.MOUSE_OUT, fadeHighlight);
}
function fadeHighlight(e:MouseEvent):void{
do{
e.target.alpha -=.2;
} while(e.target.alpha >0);
}

Thanks!
varPBR

pelkin000
04-10-2008, 03:26 PM
you will want to add the mouse out event listener from within the first function:


for each(var navButton:MovieClip in buttonArray){
navButton.scaleY = 16;
navButton.buttonMode = true;
navButton.addEventListener(MouseEvent.MOUSE_OVER, buttonHighlight);
navButton.addEventListener(MouseEvent.MOUSE_OUT, fadeHighlight);
navButton.alpha = 0;
addChild(navButton);
}


this SHOULD solve your problem... also, i suggest replacing e.target with e.currentTarget, but trace(e.target) and trace(e.currentTarget) to make sure it works.

varPBR:Tasty
04-10-2008, 03:33 PM
funny, still, the button just disappears immediately...this might have something to do with my frame rate. I think I am going to try using a tween instead...unless there are any other ideas as to why this might be happening.

Thanks for the quick reply!

varPBR

varPBR:Tasty
04-10-2008, 03:35 PM
yup, just created a tween instead, and that works (using time instead of frames)!

Thanks for the help!