Solved

Flash - create functions in loop

Posted on 2011-03-24
1
483 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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
zeroMAx challenge 20 76
How to split this in C++ 4 76
create an incrementing variable name AutoHotKey 5 56
Actionscript in Flash 3 23
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 …
A short article about a problem I had getting the GPS LocationListener working.
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
The goal of the tutorial is to teach the user how to how to record live broadcast.

744 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now