Solved

Flash - create functions in loop

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

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

Suggested Solutions

Title # Comments Views Activity
ASP/VB email question 4 72
asp Google Map 2 93
Assignment from incompatible pointer type? 2 78
Extract data from output with RegEx 1 41
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
The goal of the tutorial is to teach the user what frame rate is, how to control it and what effect it has on the video.

710 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