Ideas regarding flow control and loops

downs default_357-line at yahoo.de
Sat Nov 3 16:49:55 PDT 2007


Marco Aurélio wrote:
> Daniel Keep Wrote:
>> else would also be nice.
> 
> This would be specially usefull for implementing collisions on 2D games with bitmap-based coldefs, something along the lines of:
> 
> while(object.isCollidingWithGround())
> {
>      object.moveUp();
> } finally {
>      object.stop();
> } else {
>      object.applyGravity();
> }
> 
> While the object is colliding with the ground, move it up... Then stop it.. and if he wasn't colliding with the ground in the first place, apply gravity.

Well, technically ...
 --downs

PS:
module test17;
import std.stdio;

void extwhile(lazy bool cond, void delegate() Body, void delegate()
Finally, void delegate() Else) {
  if (!cond()) Else();
  else {
    do Body(); while (cond());
    Finally();
  }
}

void main() {
  bool colliding=true;
  int counter=0;
  extwhile(colliding,
    { writefln("Still colliding"); counter++; if (counter==3)
colliding=false; },
    { writefln("Done colliding"); },
    { writefln("Never collided"); }
  );
}



More information about the Digitalmars-d mailing list