I run into this all the time, so I assume it’s a common and quite basic functionality people use - I’m still not sure how to approach it correctly.
Say I have a large number of MovieClips acting as controller buttons, placed in an array: btns[]. I have a second array of controlled MovieClips that should be activated when the corresponding button is pressed: mcs[]. I don’t want to make a custom listener for each clip - instead, I have one listener function that correctly plays whatever MC is assigned to it.
Now, I want to simply loop through the array of buttons and assign each button a MouseEvent.CLICK listener that would activate the correct clip. Originally I just wanted to pass the index of the array I am on to the listener, so the MC with the corresponding index would be affected. But of course, you can’t pass parameters to a listener…
So, what’s the best way to get this done? I can think of three:
-
Create a custom event that would pass the index. Haven’t tried and have no idea how to do this.
-
Create a custom class that has two MCs as members, the controller and the controlled, and a function that plays the controlled clip. Then simply add the eventListener in the constructor of the class.
-
Name each controller and controlled MC in a predetermined way. Then use strings to activate the correct the correct controlled MC.
I’ve been using method 2, but I get the feeling that I am simply missing something and there must be a much easier way to accomplish this. Any suggestions?