A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: for loop problem

  1. #1
    Member
    Join Date
    Jan 2003
    Posts
    85

    for loop problem

    The following code doesn't work.
    You probably already know by seeing the code what it is I'm trying to do and what I have done wrong.

    Code:
    for (i=1; i<=4; i++) {
    	this["bt"+i].onRollOver = function() {
    		this._parent["icon"+i].reverse = false;
    		this._parent["icon"+i].play();
    	}
    }
    But just to spell it out:

    I have 4 buttons and 4 MC's on the same timeline. I want to have bt1 play icon1 onRollOver, bt2 to play icon2 onRollOver and so forth...

    But the var i always equals 5.

    I have searched and found some answers by the user silentweed but I'm apparently to dumb to understand
    What do I do ?

    Thank you

  2. #2
    Member
    Join Date
    Dec 2005
    Location
    Sweden
    Posts
    36
    Couldn't figure out the problem, but here's one way to fix it -.-

    Code:
    for (i=1; i<=4; i++) {
    	this["bt"+i].i = i;
    	this["bt"+i].onRollOver = function() {
    		this._parent["icon"+this.i].reverse = false;
    		this._parent["icon"+this.i].play();
    	}
    }
    my personal portfolio: thirtyeight.se

  3. #3
    Senior Member
    Join Date
    Feb 2005
    Posts
    149
    As regards the variable i equalling 5. If you trace i outside of and after the execution of the loop, then i will equal 5. An event handler function executes when that event occurs. At the time of execution, it grabs the current values of the variables referenced within the function. So onRollOver, i is equal to 5 in this case.

    iONx's code works works because within the loop, a new property, "i", is being defined on each of the mc's named bt1, bt2, bt3, bt4 and given the value of the i for that iteration. So bt1.i=1, bt2.i=2, bt3.i=3, bt4.i=4. Then onRollOver, the function executes referencing the current value of the i property for the mc on which it is executing. So bt3.onRollOver will play icon3 because "this" within the function refers to bt3 and bt3.i is equal to 3.
    Last edited by winterac; 06-18-2006 at 11:33 AM.

  4. #4
    Member
    Join Date
    Jan 2003
    Posts
    85
    Thank you both of you.

    Your code works as I want it, iONX.

  5. #5
    Member
    Join Date
    Jan 2003
    Posts
    85
    BTW iONX

    Can you explain why your codes work ?
    I would really like to learn instead of just relying on others everytime i run into a problem.

  6. #6
    Senior Member
    Join Date
    Feb 2005
    Posts
    149
    Well, blah. My reply of last night is largely wrong. I took a second look this morning and edited the post. Sorry for misleading.

  7. #7
    Member
    Join Date
    Jan 2003
    Posts
    85
    Thank you so much winterac

    I totally understand now - great !

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center