2 Replies Latest reply on Jul 29, 2007 8:10 AM by lichtjiang

    How to use common event handler for selected movie clips?

    lichtjiang
      I have a 50-state map in a flash movie. Each state is a movie clip.

      Goal: when mouse moves over a state or is clicked in a state, the state will be highlighted in a bright color and a small box will pop up near the state and display some information about the state.

      Question: I know I can add mouse event handler for each state movie clip. But this is simply not good since this has to be done 50 times and codes thus scattered different places. Ideally, I only want to have one script that determines where the mouse position is when events trigged and then do right things (highlight the state and display info. in a pop-up). How can this be implemented?

      Thanks!
        • 1. Re: How to use common event handler for selected movie clips?
          Greg Dove Level 4
          There are a number of ways. Which way is best depends on how you have things set up so far.

          E.g. If they have an enumerable naming convention:
          e.g. each clip is like state_0 , state_1 etc.

          Then you can loop through them and assign them all to the same mouse event handler via the loop. You would need properties other than the name of the clip to identify the state. E.g. each clip could contain its own data or the index could be a pointer to the state data (objects with state name and info properties) in a separate array.
          //state clips named
          for (var i=0;i<50;i++) {
          this["state_"+i].stateindex=i;
          this["state_"+i].onPress= statePressHandler;
          }

          var stateData:Array = [{name:"StateName,info:"this state Info"}, name:"StateName,info:"this state Info"}, etc...]

          function statePressHandler() {
          trace(this);
          trace(stateData[this.stateindex].name+"="+stateData[this.stateindex].info);
          }

          Other ways are possible too but the best approach depends on how you have named the clips and whether you're creating them with code or whether they're already on stage from authoring (my guess). If they're already on stage and they're called "Alaska" etc, then I would be inclined to put them all inside a container clip that contains nothing else other than states. It would avoid the need for an array of clip names or for checking some other specific property of each clip to determine if its a 'state' clip and not something else in a for..in loop.

          • 2. Re: How to use common event handler for selected movie clips?
            lichtjiang Level 1
            quote:

            If they're already on stage and they're called "Alaska" etc, then I would be inclined to put them all inside a container clip that contains nothing else other than states. It would avoid the need for an array of clip names or for checking some other specific property of each clip to determine if its a 'state' clip and not something else in a for..in loop.


            Yes, each state is in its own movie clip. But there is yet no container clip (which I guess is a clip that you put all states' clips in it?). So, if I create this container clip, how do I know if a click is in which state, which you said "It would avoid the need for ..."?

            Thanks!