Skip to main content
Known Participant
September 18, 2009
Answered

Use Array To apply eventListeners to numerous Buttons

  • September 18, 2009
  • 1 reply
  • 510 views

I have this function that I want to apply to 10 different MovieClips that are on the stage by using an array. I wrote the array, I just need to know how I can apply the array to the function. Thank you

var sliceArray:Array=["slice1","slice2","slice3","slice4","slice5","slice6","slice7",
"slice8","slice9","slice10"];

btn1.addEventListener(MouseEvent.MOUSE_DOWN, sliderFunction)
btn1.addEventListener(MouseEvent.MOUSE_UP, sliderFunctionRelease)

function rotateMC(num:Number)
    {
        slice1.rotation+=num;       
    }

function sliderFunction(event:Event):void
    {
        stage.addEventListener(Event.ENTER_FRAME, onEnter)
    }

   
function onEnter(event:Event):void
    {
    rotateMC(10);
    }
   
function sliderFunctionRelease(event:Event):void
    {
        stage.removeEventListener(Event.ENTER_FRAME, onEnter);
    }

This topic has been closed for replies.
Correct answer Ned Murphy

I can't see what listeners you plan to assign where with what you show, but to loop thru an array of instance names to assign things you would use the bracket/array notation to have the strings interpretted as instance names...

for(var i:uint = 0; i<sliceArray.length; i++){

     this[sliceArray].addEventListener....

}

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
September 18, 2009

I can't see what listeners you plan to assign where with what you show, but to loop thru an array of instance names to assign things you would use the bracket/array notation to have the strings interpretted as instance names...

for(var i:uint = 0; i<sliceArray.length; i++){

     this[sliceArray].addEventListener....

}

Known Participant
September 18, 2009

Thank you Mr Murphy....that helped

I think I misspoke, I just wanted to have the actions within my function applied to all the members in the array. Just wanted to do this. Thanks

var sliceArray:Array=["slice1","slice2","slice3","slice4","slice5","slice6","slice7",
"slice8","slice9","slice10"];

function rotateMC(num:Number)
    {
        for(var i:uint = 0; i<sliceArray.length; i++)
            {
     this[sliceArray].rotation+=num;       
            }
    }

Ned Murphy
Legend
September 18, 2009

You're welcome.