Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Flash - create functions in loop

Posted on 2011-03-24
1
Medium Priority
?
502 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
1 Comment
 
LVL 8

Accepted Solution

by:
ActionScript_Helper earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Screencast - Getting to Know the Pipeline

886 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