Can you break out of a FlxTypedGroup foreach?

159 views
Skip to first unread message

Jimmy JimJim

unread,
May 23, 2015, 12:33:24 PM5/23/15
to haxef...@googlegroups.com
Hi all

I'm still getting used to the Haxe syntax, so this might be a dumb question...

If I'm using a foreach call on a FlxTypedGroup, is there a way to break out of the loop? Since I'm just passing through a function to call from an invisible while loop, I'm technically not in the loop itself, I'm just in the function that is being called: 

forEach(function(enemy:FlxSprite) {  
enemy.doSomethingFacy();
break; //error: break outside loop
});


Obviously the function above doesn't need to call break, I'm just using it as an example. 

My current workaround is to add a local 'processing' variable that I set to false and wrap around the function code, but it's pretty hacky. Is there a way of naturally breaking out of the FlxTypedGroup foreach?

Sebastian Fernandez

unread,
May 23, 2015, 5:43:14 PM5/23/15
to haxef...@googlegroups.com
You can extend the group class and make a "ForEach" function that works with "T->Bool" functions, and make the loop and the break.

But, if you have smalls groups an easy solution is to set a "break" var in true when you want to finish, and your callback function will be:

function(enemy:FlxSprite){
   if(break) return;
    //the rest of the code}

This way, you won't avoid the rest of the loop but it won't do anything. 

Jimmy JimJim

unread,
May 23, 2015, 6:31:22 PM5/23/15
to haxef...@googlegroups.com
Your second suggestion is what I'm doing now, it just feels a bit messy to do. I'll look into your first suggestion - that might do it.

Cheers.

Gama11

unread,
May 24, 2015, 3:58:25 AM5/24/15
to haxef...@googlegroups.com, laserdi...@gmail.com
Why use a forEach() call instead of a loop in the first place then? for (member in group) should do the trick.
Reply all
Reply to author
Forward
0 new messages