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

2012/5/8

adding some wait time before execution

joey511 joey511

2012/5/8

#
Hey everyone, I was wondering how do I make the code below wait for 5 seconds before executing?
public void collision()
    {
      Rocket target = (Rocket) getOneIntersectingObject(Rocket.class) ;
       if( target != null)
       {
           target.setImage("Explosion.jpg");
           getWorld().removeObject( target ) ;
           Greenfoot.stop();
       }
    }
Basically this code is meant to end the game if the rock hits the rocket but seen as the rocks are randomly placed they are sometimes on top of the rock and as soon as I run it the game ends which is no fun at all :( so I want to add a line of code that makes the rocket invinsible for the first 5 seconds of the game.
danpost danpost

2012/5/8

#
It would be easier to spawn the rocket first, then spawn the rocks so that they are not near the rocket. This can still be achieved randomly with (in the world class constructor or a method it calls):
Rocket rocket = new Rocket();
int rocketX = getWidth() / 2; // or at a location of your choosing
int rocketY = getHeight() / 2; // or at a location of your choosing
addObject(rocket, rocketX, rocketY);
int numberOfRocks = 3; // or however many
int safteyRange = 100; // not within 100 pixels (or however many) of rocket
for (int i = 0; i < numberOfRocks; i++)
{
    int rockX = (Greenfoot.getRandomNumber(getWidth() - 2 * safteyRange) + rocketX + safteyRange) % getWidth();
    int rockY = (Greenfoot.getRandomNumber(getHeight() - 2 * safteyRange) + rocketY + safteyRange) % getHeight();
    addObject(new Rock(), rockX, rockY);
}
You need to login to post a reply.