-
Simple Array/Loop Question
I am trying to have my loop assign all three actions; over, out, and down, to my buttons. The problem is that the button's down functions are different than one another. So I am trying to assign it the functions name + the number it is in the loop. I would think it would look something like this.
code: var array:Array = [contentWrap.pete_email,contentWrap.wayne_email,con tentWrap.info_email];
for (var i:int = 0; i < array.length;i++){
array[i].stop();
array[i].addEventListener(MouseEvent.MOUSE_OVER, btnOver);
array[i].addEventListener(MouseEvent.MOUSE_OUT, btnOut);
array[i].addEventListener(MouseEvent.MOUSE_DOWN, btnDown+"[i]");
}
private function btnDown0(e:MouseEvent):void {
trace("pete");
}
private function btnDown1(e:MouseEvent):void {
trace("wayne");
}
private function btnDown2(e:MouseEvent):void {
trace("info");
}
-
Senior Member
for (var i:Number = 0; i < array.length; i++) {
array[i].stop();
array[i].addEventListener(MouseEvent.MOUSE_OVER, btnOver);
array[i].addEventListener(MouseEvent.MOUSE_OUT, btnOut);
array[i].addEventListener(MouseEvent.MOUSE_DOWN, this["btnDown"+i]);
}
Use that for your loop and it'll work. Let me know if you need anything else.
-
That worked perfect, thanks so much.
-
Senior Member
No problem, happy to help.
-
Senior Member
The code's still a bit dirty for a couple of reasons. For one thing, you don't need to call separate functions for the click on each button. And another nicer way of doing it would be to make all the event listeners point to the same function:
Code:
array[i].name = String(i); //you'll see why I added this later.
array[i].addEventListener(MouseEvent.MOUSE_OVER, btnFn);
array[i].addEventListener(MouseEvent.MOUSE_OUT, btnFn);
array[i].addEventListener(MouseEvent.MOUSE_DOWN, btnFn);
Okay, now they all call btnFn().
Code:
function btnFn(evt:MouseEvent):void {
switch (evt.type) {
case "mouseOver":
//do my mouseOver on evt.currentTarget (the button)
break;
case "mouseOut":
///etc.
break;
case "mouseDown":
//here, evt.currentTarget is the button that was clicked,
//so we can use it however we want. If it has a .name parameter,
////////////we could check for that. Like:
if (evt.currentTarget.name == 0) {
//whatever you want for that button.
}
break;
}
}
-
That's a pretty cool way for simple buttons. But what if you wanted to make a toggle type of menu where you would disable a button after it was clicked and make it stay in it's highlighted position by removing it's roll out listener?
-
evt.currentTarget.
Thanks, I spent hours trying to figure this one out.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|