Solved

Flash - create functions in loop

Posted on 2011-03-24
1
488 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 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This is an explanation of a simple data model to help parse a JSON feed
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
The goal of the tutorial is to teach the user how to how to record live broadcast.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

806 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