SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Event handler scope?

    Hi!

    I have an array of movieclips and for each one I want to assign an onRelease-function with a specific parameter, a counter. Something like:

    Code Actionscript:
    for (var i = 0; i < _icons.length; i++) {
     
        _icons[i].onRelease = function() {
            _hostInstance.myFunction(i);
        }
    }

    But that doesn't work since the variable 'i' isn't interpreted until the onRelease is invoked, i.e. it always has the value of _icons.length. Is there any way to solve this without doing something like the loop unrolling below?


    Code Actionscript:
        _icons[0].onRelease = function() {
            _hostInstance.myFunction(0);
        }
     
        _icons[1].onRelease = function() {
            _hostInstance.myFunction(1);
        }
     
        _icons[2].onRelease = function() {
            _hostInstance.myFunction(2);
        }
     
        _icons[3].onRelease = function() {
            _hostInstance.myFunction(3);
        }

    I use AS2.0 and would very much appreciate some help here.

  2. #2
    Community Advisor silver trophy

    Join Date
    Nov 2006
    Location
    UK
    Posts
    2,554
    Mentioned
    40 Post(s)
    Tagged
    1 Thread(s)
    Code:
    for (var i = 0; i < _icons.length; i++) {
     
        _icons[i].id = i;
        _icons[i].onRelease = function() {
            _hostInstance.myFunction(this.id);
        }
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •