|
-
DOT-INVADER
break a loop from a function?
basically i want to break a for loop from an external function, like that:
PHP Code:
for (i=0; i<=10; i++) {
test(5);
trace(i);
}
function test(value) {
if (i == value) {
break;
}
}
is there a way? i want to avoid that:
PHP Code:
for (i=0; i<=10; i++) {
if (i == 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=0; i<=10; i++) {
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.
-
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.
-
ism
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
-
Senior Member
I know its silly but I am hear to learn things.
What would I use something like this for?
-
Senior Member
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;
}
}
-
DOT-INVADER
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.
-
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]
-
-
DOT-INVADER
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
-
ism
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
-
DOT-INVADER
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...
-
ism
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
-
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!
-
Participant
hmmm...
Is this ok, or am I completely missing the point?:
for (i=1;i<=10;i++){
if (function(i)){
break;
}
}
-
383,890,620 polygons
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|