5 Replies Latest reply on Jul 30, 2007 1:16 PM by kglad

    Event Handler Scope Issue

    Almal
      Hi. I'm pretty new at AS and I'm having some trouble understanding event handler scope. I tried searching the forums, but there doesn't seem to be any threads on this particular problem. I'm trying to dynamically set onRelease event handlers for movie clips created in an onLoad event handler attached to an XML object. Unfortunately, when the onRelease functions are called, the variables seem to have passed out of scope (I get "undefined" as the value). I've attached the code below. If anyone has any suggestions, they'd be greatly appreciated.

      Thanks!
      -------
        • 1. Re: Event Handler Scope Issue
          kglad Adobe Community Professional & MVP
          nameNavClip["NavMviClp" + i] has no way to know it's the i-th movieclip unless you store a value of i somewhere (that's retrievable) before the for-loop increments.

          try:

          • 2. Re: Event Handler Scope Issue
            Almal Level 1
            Thanks very much kglad, working as expected now! Just so I know for future reference:

            The onRelease function was existing local to the movie clip's scope, so setting ivar local to the movie clip (and assigning it the current value of i when it's created) allowed the onRelease function to reference it and select the appropriate value from the array, correct? (I apologise if that question wasn't totally clear).

            Thanks again.
            • 3. Re: Event Handler Scope Issue
              kglad Adobe Community Professional & MVP
              the issue is not scope. the issue is the value of i when your movieclip button is released.

              if you use trace(i) within your onRelease handler you'll see it's defined (and the last value of your for-loop). so, it's not a scope issue.

              the issue is, you don't want the value of i when the button is released. you want the value of i when the onRelease handler was created.
              • 4. Re: Event Handler Scope Issue
                Almal Level 1
                I understand now. The value of i at that point in the code is the value of i in the last iteration of the loop which is one more than the length of the array. Therefore, when I tested the movie, my onRelease was trying to retrieve an array element that didn't exist so I got "undefined" as the value.

                I think I got it now, thanks again!
                • 5. Re: Event Handler Scope Issue
                  kglad Adobe Community Professional & MVP
                  you're welcome.