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

2012/4/23

wait();

Drew Drew

2012/4/23

#
This code is supposed to add a 'DemonSpider' to the world every five seconds: public void addEnemy() { int y = Greenfoot.getRandomNumber(getHeight()); addObject(new DemonSpider(),getWidth(),y); wait(5); } I get this in the output: unreported exception java.lang.InterruptedException; must be caught or declared to be thrown
nccb nccb

2012/4/23

#
Rather than fix your error, what you actually need to do is code this differently. If you wait in a Greenfoot actor, it pauses the entire game for 5 seconds before continuing. Which is not what you want here. What you should do instead is add a variable to keep track of time, e.g. in your act() method:
long lastAdded = System.currentTimeMillis();

public void act()
{
    long curTime  = System.currentTimeMillis();
    if (curTime >= lastAdded + 5000) //5000ms = 5s
    {
        int y = Greenfoot.getRandomNumber(getHeight());
        addObject(new DemonSpider(),getWidth(),y);
        lastAdded  = curTime;
    }
}
That will add a new demon spider every 5 seconds, but without delaying the whole of your scenario.
davmac davmac

2012/4/23

#
Also, in general, don't use wait(...); use Greenfoot.delay(...) if you need a delay. As nccb says, it will delay the whole scenario however.
Duta Duta

2012/4/23

#
If Greenfoot.delay(...)'s are really needed in some code for reason, but you don't want the whole scenario pausing, I suppose you could do:
while(shouldContinue)
{
	//...
	for(Actor a : (List<Actor>)getWorld().getObjects(null))
		if(a != this) a.act();
	Greenfoot.delay(1);
}
davmac davmac

2012/4/23

#
As a matter of style I would also include the { }s after the 'for' even though it is not strictly necessary in this case. The technique will interfere a little with Greenfoot's input handling (keyboard/mouse) though it might be ok for some scenarios. Finally, this technique will only work with one actor - once another one tries to do the same thing, strange things may start to happen (actor A will make actor B act, and actor B will then try to make actor A act).
Duta Duta

2012/4/24

#
davmac wrote...
As a matter of style I would also include the { }s after the 'for' even though it is not strictly necessary in this case. The technique will interfere a little with Greenfoot's input handling (keyboard/mouse) though it might be ok for some scenarios. Finally, this technique will only work with one actor - once another one tries to do the same thing, strange things may start to happen (actor A will make actor B act, and actor B will then try to make actor A act).
Oh fair point, I hadn't thought about the possible recursiveness of it. I suppose you could have a List of actors that are currently delaying... I'm not sure how you'd get around the input handling issues though. Also if anyone actually wanted to use my code it should have a getWorld().act(); in it - I forgot to include it.
You need to login to post a reply.