A Flash Developer Resource Site

Results 1 to 14 of 14

Thread: Looping / Incrementing onRelease Function

  1. #1
    Junior Member
    Join Date
    Feb 2009
    Posts
    8

    Cool Looping / Incrementing onRelease Function

    Hi. Just getting up to speed on looping and incrementing variables. A real time saver.

    I have a onRelease function that I would like to apply to several button objects:

    phase1a.but1.onRelease = function() {
    p=0;
    thisImage();
    }

    I need the p variable as well as the target but1 to increment. This is my first attempt:

    for (n=1; n<=23; n++) {
    phase1a.but[n].onRelease = function() {
    p=[n+1];
    thisImage();
    trace (p);
    }
    trace (n);
    }

    I get a good trace on n showing that the incrementing is successful but nothing on p when the button is clicked and no execution of the function thisImage(). Not getting any errors either. Can you not loop an onRelease statement?

    Thanks in advance.

  2. #2
    Ryan Thomson EvolveDesigns's Avatar
    Join Date
    Oct 2001
    Location
    British Columbia
    Posts
    3,338
    try this:

    phase1a["but"+n].onRelease = function() {
    Evolve Designs Interactive Media
    the natural selection

  3. #3
    Junior Member
    Join Date
    Feb 2009
    Posts
    8
    Thanks for the reply. I actually had tried that already but it threw an error because I forgot to omit the "." before the first bracket [.

    Now seeing an issue with the p variable not incrementing. trace (p); just yields 25 no matter which button is pressed.

    for (n=1; n<=23; n++) {
    phase1a["but"+n].onRelease = function() {
    p=n+1;
    thisImage();
    trace (p);
    }
    }

    J

  4. #4
    Ryan Thomson EvolveDesigns's Avatar
    Join Date
    Oct 2001
    Location
    British Columbia
    Posts
    3,338
    that's because you need to do something with p as you did with n by assigning it to your buttons. otherwise once the for loop is done (which is next to instant) it will have the highest possible number.

    how is p being used? (wow that sounds odd)
    Evolve Designs Interactive Media
    the natural selection

  5. #5
    Junior Member
    Join Date
    Feb 2009
    Posts
    8
    Yeah. I get it now. This is getting a little convoluted for my right brain.

    p stands for the current image of a slide show. Each button object is a thumbnail. Clicking on a thumb should call the corresponding image represented by p so I get that p has to increment somehow within the onRelease handler. Maybe I'll try to put another loop in the onRelease handler.

    Thanks again.

    J

  6. #6
    Junior Member
    Join Date
    Jan 2002
    Posts
    4
    Coincidentally, I'm running into the same problem. I also tried nesting another loop inside the onRelease handler but it didn't work. Did you ever solve it? Thanks.

  7. #7
    Junior Member
    Join Date
    Feb 2009
    Posts
    8
    Nope. Same problem. With every loop, the variable changes and wont stay fixed within the button object. Maybe if it's specified as _local?

    Looking into incrementing the variable name as well so it wont get overwritten...

    Anything to keep from writing 60 onRelease statements

    J

  8. #8
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Code:
    for (n = 1; n <= 23; n++) {
    	phase1a.but[n].num = n
    	phase1a.but[n].onRelease = function() {
    		p = (this.num + 1);
    		thisImage();
    		trace(p);
    	};
    }
    Last edited by dawsonk; 03-10-2009 at 03:46 PM.

  9. #9
    Junior Member
    Join Date
    Feb 2009
    Posts
    8

    Brilliant!!!

    I thought about trying to pull the variable back out of the path using target path(this) but this is much better. Actually here is the code that worked:

    Code:
    for (n=1; n<=phase1total; n++) {
    	phase1a["but"+n].num = n
    	phase1a["but"+n].onRelease = function() {
    		p = [this.num-1];
    		thisImage();
    		trace (p);
    	}
    }
    Thanks,

    J

  10. #10
    Junior Member
    Join Date
    Jan 2002
    Posts
    4
    That seems to work for me as well - up to a point. For some reason I'm having a hell of a time trying to target a movieclip from within the function:

    PHP Code:
    for (n=0n<=15n++) {
        
    number_array["numbtn"+n].num n*-900;
        
    number_array["numbtn"+n].onRelease = function() {
            
    = [this.num];
                
    container_mc._x p;
            
    trace(p);
        };    

    If I substitute a number (ie. 400) instead of the variable 'p' it works. But for some reason the variable doesn't. However, the trace DOES work. Weird.

    I might just resort to doing it the painful way. I only have to do 15 on releases not 60 like you! ;-)

  11. #11
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Code:
    for (n = 0; n <= 15; n++) {
    	number_array["numbtn" + n].num = n * -900;
    	number_array["numbtn" + n].onRelease = function() {
    		p = this.num;
    		this._parent.container_mc._x = p;
    	};
    }

  12. #12
    Junior Member
    Join Date
    Jan 2002
    Posts
    4

    Talking

    Quote Originally Posted by dawsonk View Post
    Code:
    for (n = 0; n <= 15; n++) {
    	number_array["numbtn" + n].num = n * -900;
    	number_array["numbtn" + n].onRelease = function() {
    		p = this.num;
    		this._parent.container_mc._x = p;
    	};
    }
    Thanks, but it didn't work. Ugh. Pretty sure it's not a targeting error (i've tried it a number of different ways including _root). It's one of those I can spend the next 4 hours on it or I can move on. Unfortunately it'll probably be the later. Appreciate the help though. Thanks again.

  13. #13
    Junior Member
    Join Date
    Feb 2009
    Posts
    8

    Another Solution...

    Yeah, I actually ended up going another way because there was something strange going on the the variable P that was messing up other parts of my code.

    I ended up actually using the button path and pulling the variable I needed out of it. Don't laugh, it's not pretty.

    Code:
    for (n=1; n<=phase1total; n++) {
    	phase1a["but"+n].onRelease = function() {
    		current=targetpath(this);
    		p = current.substr(current.lastIndexOf('t')+1,current.length)-1;
    		thisImage();
    	}
    }
    The path that's returned by targetpath() is "_level0.phase1a.but3" so I just stripped everything else of and made it my variable and, for my purposes, I had to subtract 1.

    I know, yikes. but it works.

    J

  14. #14
    Junior Member
    Join Date
    Jan 2002
    Posts
    4
    Ouch. Brain freeze. Glad you got it work. I'll have to tackle it again in my next project. At least I know I'm getting closer! Thanks.

    Ron

Tags for this Thread

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