5 Replies Latest reply on Nov 22, 2016 10:35 AM by ClayUUID

    Dynamically assigning event listeners to movie clips? (Canvas)

    anton9800 Level 1

      Hello!  I am trying to come up with a way to dynamically assign "click" event listeners to a number of movie clips.

       

      the movie clips are arranged in the following manner:

       

      ParentMovieClip

      |_childMovieClip_1
      childMovieClip_2
      childMovieClip_3

       

      my current approach is this:

       

      1. iterate through a parentMovieClip and push all of its children inside an array (that gives me a number of objects I'll turn into buttons).

      2. iterate through the array, and assign a name inside the parent movie clip to a temporary variable, then assign event listeners to it.  The code is below:

       

              // temporary variable holding address of current movie clip

      for (var i = 0; i < movieClipArray.length; i++) {              

          var tempVariable = "this.parentMovieClip.childMovieClip_" + (i+1);

      // here I assign listeners to movieclip.  This does turn my childMovieClips into buttons, but I cannot distinguish which clip was clicked using a switch or an if statement, since there are no attributes to check for

           eval(tempVariable).addEventListener("click", clickHandler.bind(this));             

                    // this gives me an actual object, though it's lacking all attributes except for ID

         var tempObject = eval(tempOpenPopup);  

                     // so I assign a name to it manually, in hopes of being able to tell the buttons apart when I click them

          tempObject.name = i.toString();

                   // but I cannot figure out a way to assign an event listener to this tempObject.

      }

       

      So, while I am partially successful, I am out of ideas as to how to distinguish between which movie clip was clicked, as the name is not being passed.  Any ideas would be greatly appreciated!

       

      Thank you!