A Flash Developer Resource Site

Results 1 to 15 of 15

Thread: break a loop from a function?

  1. #1
    DOT-INVADER marmotte's Avatar
    Join Date
    May 2002
    Location
    Dot-Switzerland
    Posts
    2,601

    break a loop from a function?

    basically i want to break a for loop from an external function, like that:
    PHP Code:
    for (i=0i<=10i++) {
        
    test(5);
        
    trace(i);
    }
    function 
    test(value) {
        if (
    == value) {
            break;
        }

    is there a way? i want to avoid that:
    PHP Code:
    for (i=0i<=10i++) {
            if (
    == 5) {
            break;
        }
        
    trace(i);

    why avoid that above code if it's so simple? because in my game it looks more like:
    PHP Code:
    for (i=0i<=10i++) {
     
    function_a(x)
     
    function_b(x)
     
    function_c(x)
     
    function_d(x)
     
    function_e(x)
     ...
    etc

    do you see the point? if the first function (here function_a) is true, then the for loop is broken and all the other functions below ignored...

    maybe there's a way to target that for loop with that break command? something like _parent.break;, or give that for loop a name...

    thanks in advance for the infos.

  2. #2
    avatar free
    Join Date
    Jul 2002
    Location
    UK
    Posts
    835
    maybe use a switch statement?

    code:

    for (var i = 0; i<11; i++) {
    trace("for loop - "+i);
    var x = 1;
    switch (x) {
    case 1 :
    trace("case 1");
    if (function_a(i)) {
    break;
    } else {
    x++;
    }
    case 2 :
    trace("case 2");
    if (function_b(i)) {
    break;
    } else {
    x++;
    }
    case 3 :
    trace("case 3");
    if (function_c(i)) {
    break;
    } else {
    x++;
    }
    case 4 :
    trace("case 4");
    if (function_d(i)) {
    break;
    } else {
    x++;
    }
    }
    }



    I tried this. When it breaks it will break the for loop and move to the next i iteration. It just looks a bit long though - i'm not sure if this will help at all. Try it and see

    [EDIT]
    Sorry, just looked at your post again, i don't think this is quite the same as yours, but maybe you can convert the idea?
    [/EDIT]

    [EDIT 2]
    On 3rd thoughts... I think maybe this will work with what you want to do. Hmm, tired.
    [/EDIT 2]
    Last edited by jonmack; 08-08-2003 at 09:06 PM.
    jonmack
    flash racer blog - advanced arcade racer development blog

  3. #3
    ism BlinkOk's Avatar
    Join Date
    Aug 2001
    Location
    , location, location
    Posts
    5,002
    mabey you could put the functions in a loop as well;
    Code:
    functions = [ "function_a", "function_b", "function_c" ];
    for(i=0;i<10;i++) {
     for(j=0;j<functions.length;j++) {
       r = eval(functions[j])(x)
       if (r == false)
         break;
     }
     if (r == false)
       break;
    }
    the highlighted code will call the function named in the string. it will prolly be a little slower coz of the additional loop but if your lookin for simple coding it might work.
    ps: a nifty side effect is you can change/add/delete the order of functions called simply by changing the make-up of the array.
    Graphics Attract, Motion Engages, Gameplay Addicts
    XP Pro | P4 2.8Ghz | 2Gb | 80Gb,40Gb | 128Mb DDR ATI Radeon 9800 Pro

  4. #4
    Senior Member MiSSenLinX's Avatar
    Join Date
    Jan 2003
    Location
    Perth, Australia
    Posts
    260
    I know its silly but I am hear to learn things.
    What would I use something like this for?

  5. #5
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    Why wouldnt this work?
    code:

    breakme=false;
    for (i=0; i<=10 and !breakme; i++) {
    test(5);
    trace(i);
    }

    function test(value) {
    if (i == value) {
    breakme=true;
    }
    }


  6. #6
    DOT-INVADER marmotte's Avatar
    Join Date
    May 2002
    Location
    Dot-Switzerland
    Posts
    2,601
    Originally posted by tonypa
    Why wouldnt this work?
    code:

    breakme=false;
    for (i=0; i<=10 and !breakme; i++) {
    test(5);
    trace(i);
    }

    function test(value) {
    if (i == value) {
    breakme=true;
    }
    }

    pffffffff.... that's too easy! so simple, so simple, why didn't we think about that little addition... thanks a lot tonypa!

    oh, and thanks also you guys...

    missenlix
    well, it's complicated... in my case i'm creating a fighting game which is quite well evolving... there are a lot of values, and a lot of functions used in order to have a quality playability (well, that's my goal indeed)...
    if you want, i can explain better, but it may be long, you are warned ^^
    Last edited by marmotte; 08-09-2003 at 08:05 PM.

  7. #7
    avatar free
    Join Date
    Jul 2002
    Location
    UK
    Posts
    835
    Tonypa,

    What that will do is stop the entire loop from continuing. What marmotte wants is to skip the current itteration only, which is done by using "break".

    Good ideas though, you too blink.

    [EDIT]
    Oh wait, i've misread yet again. Maybe that IS what marmotte wants
    [/EDIT]
    jonmack
    flash racer blog - advanced arcade racer development blog

  8. #8
    avatar free
    Join Date
    Jul 2002
    Location
    UK
    Posts
    835
    Ah man i got confused on this one

    It's "continue", not "break" that skips the rest of the current itteration ins't it?! All hail king tonypa

    P.S.

    But wouldn't it be

    code:

    function test(value) {
    if (value == value_to_test_against) {
    breakme = true;
    }
    }



    ?

    Unless i is global. Just nitpicking
    jonmack
    flash racer blog - advanced arcade racer development blog

  9. #9
    DOT-INVADER marmotte's Avatar
    Join Date
    May 2002
    Location
    Dot-Switzerland
    Posts
    2,601
    jonmack, that's exactly what i needed... maybe i wasn't clear enough with my first post. but since the for loop (full of functions) stops, that's all i want. thanks again

  10. #10
    ism BlinkOk's Avatar
    Join Date
    Aug 2001
    Location
    , location, location
    Posts
    5,002
    Code:
    breakme=false;
    for (i=0; i<=10 and !breakme; i++) {
            test(5);
            trace(i);
    }
    
    function test(value) {
            if (i == value) {
                    breakme=true;
            }
    }
    just a note on this method. the for loop will only test for breakme at the top of the loop so if you have multiple functions;
    Code:
    breakme=false;
    for (i=0; i<=10 and !breakme; i++) {
            test_a(5);
            test_b(5);
            test_c(5);
            test_d(5);
    
    }
    then you will need to test for breakme after each function;
    Code:
    breakme=false;
    for (i=0; i<=10 and !breakme; i++) {
            test_a(5);
            if (breakme == true)
              break;
            test_b(5);
            if (breakme == true)
              break;
            test_c(5);
            if (breakme == true)
              break;
            test_d(5);
    }
    Graphics Attract, Motion Engages, Gameplay Addicts
    XP Pro | P4 2.8Ghz | 2Gb | 80Gb,40Gb | 128Mb DDR ATI Radeon 9800 Pro

  11. #11
    DOT-INVADER marmotte's Avatar
    Join Date
    May 2002
    Location
    Dot-Switzerland
    Posts
    2,601
    yes, very true...

    i'll do some tests later with my file.
    you had very good ideas on this one, people.

    too bad there's not some kind of _parent.break like function directly though...

  12. #12
    ism BlinkOk's Avatar
    Join Date
    Aug 2001
    Location
    , location, location
    Posts
    5,002
    i don't wanna harp on about this but i just got another idea about what you want or if anyone else is interested.
    it's called daisy chaining and would involve changing each function in your loop a little bit.
    here is the code;
    Code:
    fct_table = [ "fct_end", "fct_e", "fct_d","fct_c","fct_b","fct_a" ];
    //
    trace("Perform all functions");
    fail=null;
    for(i=0;i<10;i++)
    	if (eval(fct_table.pop())(val) == false)
    		break;
    //
    fct_table = [ "fct_end", "fct_e", "fct_d","fct_c","fct_b","fct_a" ];
    fail=fct_table[random(fct_table.length - 1) + 1];
    trace("Break at function "+fail);
    for(i=0;i<10;i++)
    	if (eval(fct_table.pop())(val) == false)
    		break;
    //
    function	fct_a(val) {
    	if (fail == "fct_a") {
    		trace("fct_a will fail");
    		return(false);
    	} else {
    		trace("fct_a will succeed");
    		return(eval(fct_table.pop())(val));
    	}
    }
    //
    function	fct_b(val) {
    	if (fail == "fct_b") {
    		trace("fct_b will fail");
    		return(false);
    	} else {
    		trace("fct_b will succeed");
    		return(eval(fct_table.pop())(val));
    	}
    }
    //
    
    function	fct_c(val) {
    	if (fail == "fct_c") {
    		trace("fct_c will fail");
    		return(false);
    	} else {
    		trace("fct_c will succeed");
    		return(eval(fct_table.pop())(val));
    	}
    }
    //
    
    function	fct_d(val) {
    	if (fail == "fct_d") {
    		trace("fct_d will fail");
    		return(false);
    	} else {
    		trace("fct_d will succeed");
    		return(eval(fct_table.pop())(val));
    	}
    }
    //
    function	fct_e(val) {
    	if (fail == "fct_e") {
    		trace("fct_e will fail");
    		return(false);
    	} else {
    		trace("fct_e will succeed");
    		return(eval(fct_table.pop())(val));
    	}
    }
    //
    function	fct_end(val) {
    	return(true);
    }
    
    stop();
    if you copy/past this to frame 1 of an empty movie and run it in the authoring environment you can see the results.
    the idea is each function will call the next function in the list if it's task is successfull. if a function fails for any reason then it should return false which will cause the parent loop to fail.
    now if the chain of functions is fixed you don't need to use the eval()/function table stuff at all;
    Code:
    // 
    trace ("Perform all functions");
    fail = null;
    for (i=0; i<10; i++) {
    	if (fct_a(val) == false) {
    		break;
    	}
    }
    // 
    letters = ["a", "b", "c"];
    fail = "fct_"+letters[random(letters.length)];
    trace ("Break at function "+fail);
    for (i=0; i<10; i++) {
    	if (fct_a(val) == false) {
    		break;
    	}
    }
    // 
    function fct_a (val) {
    	if (fail == "fct_a") {
    		trace ("fct_a will fail");
    		return (false);
    	} else {
    		trace ("fct_a will succeed");
    		return (fct_b(val));
    	}
    }
    // 
    function fct_b (val) {
    	if (fail == "fct_b") {
    		trace ("fct_b will fail");
    		return (false);
    	} else {
    		trace ("fct_b will succeed");
    		return (fct_c(val));
    	}
    }
    // 
    function fct_c (val) {
    	if (fail == "fct_c") {
    		trace ("fct_c will fail");
    		return (false);
    	} else {
    		return(true);
    	}
    }
    // 
    stop();
    now these functions are prolly called from other parts of the movie so you may need some additional code so that the daisy chain can be turned on or off.
    Graphics Attract, Motion Engages, Gameplay Addicts
    XP Pro | P4 2.8Ghz | 2Gb | 80Gb,40Gb | 128Mb DDR ATI Radeon 9800 Pro

  13. #13
    avatar free
    Join Date
    Jul 2002
    Location
    UK
    Posts
    835
    Nice idea Blink, never heard of that before. Don't care that you think that you're talking to no-one, and that a solution has been found! There's always more than one solution. People will read this and learn something new, like i just did. Thankyou!
    jonmack
    flash racer blog - advanced arcade racer development blog

  14. #14
    Participant jide's Avatar
    Join Date
    May 2003
    Posts
    264
    hmmm...
    Is this ok, or am I completely missing the point?:


    for (i=1;i<=10;i++){
    if (function(i)){
    break;
    }
    }
    glhf

  15. #15
    383,890,620 polygons nGFX's Avatar
    Join Date
    Oct 2002
    Location
    Germany / Ruhrgebiet
    Posts
    901
    just to add my dumb comment:


    Code:
    for (var i=0; (i<=10 && !functionToCall(i)); i++) {
       // code here
    }
    
    
    function functionToCall(VarToCheck) {
    
       var ReturnValue = false;
    
       // code here
       if (WhateverToCheck) {
           ReturnValue = true;
       }
    
       return ReturnValue; 
    
    }


    <olli/>

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