5 Replies Latest reply on Oct 13, 2007 8:05 AM by FlashLobo

    Trouble with variable scope or reference (AS2)

      Hello !

      The simple code below maps onRelease functions to 8 buttons.
      The goal is to load the corresponding image into a single central imagePanel whenever a button is clicked.

      There are 8 instances of the same MC on the scene (named g0 to g7), each containing a button (btnShow)

      The "downloadImage()" function loads the image when triggered by the onRelease event.
      The path to each image file is taken from the _global.paintings Array (url property).

      The problem:
      It looks like that, during the assignment, a link is made to the "reference" of the 'i' variable, instead of its VALUE.
      So, at run time, 'i' equals 8 (value at the end of the loop) for EVERY downloadImage function, instead of the incremented value ( 0 to 7)...

      Thus resulting on every button loading the same image, instead of the corresponding image (taken from the url property in _global.paintings[0] to [7])

      Well, not easy to explain properly, I hope you'll understand what I mean...

      Thanks a lot for your help, I'm pretty lost here !