Hypesol EA
Hypesol EA

Reputation: 21

Apply EventListner on Multiple MovieClip Instances

I have 10 movielclips on stage with name of "mc1,mc2,mc3....". I want to apply one eventlistner on all and I was trying to call using variable but its not working when I can directly with instance name it works.

can anyone help that how can i apply one function on multiple movieclcip instances

//var mc ="mc1";
var mc = this["mc" + 1];

mc.addEventListener(MouseEvent.CLICK, testFunction);

function testFunction(e:MouseEvent):void
{
    trace("Seconds elapsed: "+ e.target.name);
}

Upvotes: 1

Views: 24

Answers (1)

influjensbahr
influjensbahr

Reputation: 4078

If your Movieclips are named mc1, mc2 etc, you can just loop through them like this:

for(var i = 1; i <= 10; i++) {
   this["mc" + i].addEventListener(MouseEvent.CLICK, testFunction);
}

Keep in mind that accessing Movieclips by their name like this can lead to problems at runtime if the names change. It will also not give you any errors when compiling because this is all checked and executed at runtime. So debugging this might be a pain.

Upvotes: 1

Related Questions