This site requires JavaScript, please enable it in your browser!
Greenfoot back
RedPhoneBooth
RedPhoneBooth wrote ...

2012/4/18

What does the delay() method do?

RedPhoneBooth RedPhoneBooth

2012/4/18

#
I know it stops the program, but does it stop all executions? or just the ones within the class or method its placed? I'm trying to make my program wait for a short amount of time (like 0.5 seconds) for my adventure game, so that when space is pressed, it adds the swoosh, waits for a short amount of time, then removes it and waits for the user to release the space bar. also its a greenfoot method right? how would I do the same thing in pure java?
danpost danpost

2012/4/18

#
Actually, it does not stop the program; nor does it stop all executions. I guess this is a matter of terminology, though. The delay(int) method just runs a dummy loop (that does not do a thing; except passes time) until that time specified has elapsed (so, here, it is executing something and the program is not stopped). It does, however, make it appear that the program is paused; that no actors will be moving and no interaction will be responded to until that time has elapsed. You will need to be a bit more specific as to what you actually want. Is it, that you want everything to stop until the time has elapsed AND the user releases the spacebar? If not, exactly what is it you are trying to do? Again, be thorough and specific.
RedPhoneBooth RedPhoneBooth

2012/4/18

#
Well I want to force the swoosh to be removed from the world after a certain amount of time. I want the swoosh to appear once you've pressed space, but once a certain amount time has passed, for it to be removed. also in this time I want no other swooshes to be created. (The swoosh by the way is my half-assed form of combat, cause my fighting sprites looked terrible :) so the swoosh will appear when the space bar is pressed, deal damage with anything that's touching it, and then disappear) but I don't want the whole program to stop for a small amount of time, everything should still be running as this all happens.
danpost danpost

2012/4/18

#
In this case, you need to add an object boolean variable, let us call it 'swooshing' in the actor's class:
private boolean swooshing = false;
Then, in the act() method you will need a few checks 1) if swooshing and either the spacebar is down or the swoosh object is still there: do not do anything 2) if swooshing and the spacebar is not down and the swoosh object is gone: reset swoosh 3) if not swooshing and spacebar is down: begin swooshing
if (swooshing && (Greenfoot.isKeyDown("space") || getOneIntersectingObject(Swoosh.class) != null)) return;
if (swooshing && !Greenfoot.isKeyDown("space") && getOneIntersectingObject(Swoosh.class) == null) swooshing = false;
if (!swooshing && Greenfoot.isKeyDown("space"))
{
    // add swoosh object, play sound, whatever
    swooshing = true;
}
That would cover the before, during, and after for what you want.
You need to login to post a reply.