Solved

Flash - create functions in loop

Posted on 2011-03-24
1
498 Views
Last Modified: 2012-05-11
Hey guys,

I have the following code, which is not so pleasant to work with...
function ButtonOneOut(e:MouseEvent){
	btnOutFunction(e);
}
function ButtonTwoOut(e:MouseEvent) {
	btnOutFunction(e);
}
function ButtonThreeOut(e:MouseEvent) {
	btnOutFunction(e);
}
function ButtonFourOut(e:MouseEvent) {
	btnOutFunction(e);
}
function ButtonFiveOut(e:MouseEvent) {
	btnOutFunction(e);
}
function ButtonSixOut(e:MouseEvent) {
	btnOutFunction(e);
}

Open in new window


So to avoid this, how can I create multiple functions in a loop?
I've got this array already
var buttons:Array = ["ButtonOne","ButtonTwo","ButtonThree","ButtonFour","ButtonFive","ButtonSix"];

Open in new window

It would help me a lot :)
0
Comment
Question by:ErnstJacobs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 8

Accepted Solution

by:
ActionScript_Helper earned 500 total points
ID: 35213705
It looks like you have added event listener's to all 6 buttons, and for all handlers you want to call the same function.

I am assuming all these buttons are in one parent.
So here you go:
=====================
function addListeners():void
{
  var parent:DisplayObjectContainer = this as DisplayObjectContainer; //here you can replace "this" with the parent.

  // I am assuming all this buttons are sprite, or needed you can typecast it with whatever type you have
  var target:Sprite;
  for (var btnIdx:int = 0; btnIdx < buttons.length; btnIdx++)
  {
    target = parent.getChildByName(buttons[btnIdx]) as Sprite; // you can use other type as per need instead of sprite
    target.addEventListener(MouseEvent.MouseOut, mouseOutHandler);
  }
}

function mouseOutHandler(e:MouseEvent):void
{
  btnOutFunction(e);
}

Let me know your finding.
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Make the most of your online learning experience.
The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.
Simple Linear Regression

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question