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

2015/2/9

time scale of the act method not linear?

efe efe

2015/2/9

#
I want to create new actors (meteorite hits). Between two meteorite hits I want a certain time delay. I used the various options offered in the discussion and they all work. What I couldn't achieve though is to create the hits on a linear time scale, i.e. a new hit every 5 seconds or so. What happens is that after the first three or four hits I get 2 at a time then 4 and so on. Thus my question to the geeks: is the act method on a linear time scale? If no, how can I create a linear behaviour? here is the code I used to create the actor:
public void dropMeteorite(){
      
            MeteoriteHit newHit = new MeteoriteHit();
            int xPos = 25+Greenfoot.getRandomNumber (550);
            int yPos = 25+Greenfoot.getRandomNumber (650);
          
            getWorld().addObject(newHit, xPos, yPos);
}
The "dropMeteorite" method is in the act method of the MeteoriteHit class. When I put it there without any of the delay methods and set the greenfoot timer to very slow, I observe the "non linear" behaviour described above. I am using Greenfoot 2.4.0. with oracle java and a linux mint operating system.
danpost danpost

2015/2/9

#
The problem is that each MeteoriteHit object will execute its own act method. So, the number of object of that type always doubles. One solution: create the objects via your World subclass act method.
davmac davmac

2015/2/9

#
The "dropMeteorite" method is in the act method of the MeteoriteHit class.
So, your meteorites create new meteorites? Then of course the population growth is exponential, because the act() method will be called for each existing meteorite. Put it this way, if I have 1 meteorite, then it will create 1 more meteorite; I now have 2. The next round, both meteorites will create another, so I have 4. And so on (it doubles each time). If you want to create meteorites at a constant linear rate, put the code to create new meteorites in your world subclass instead.
efe efe

2015/2/9

#
...embarrassed! Thanks guys for pointing out the obvious to me! I hope my students don't find this.
You need to login to post a reply.