I want the program put an object in the World every 5 seconds or whatever elapsed time I set.
I created a method Act within the Dojo class but I cannot figure this out.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Karate fighter will kick watermelons * * @author Tom * @version 1.0 */ public class Dojo extends World { long initialTime = System.currentTimeMillis(); long elapsedTime = 0 ; /** * Constructor for objects of class Dojo. * */ public Dojo() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super ( 600 , 400 , 1 ); addObject( new Standing(), 250 , 270 ); WaterMelon w1 = new WaterMelon(); addObject(w1, 5 , 180 ); WaterMelon w2 = new WaterMelon(); addObject(w2, 5 , 250 ); } public void Act(){ addMelonToWorld( 5000 ); } public void addMelonToWorld( long elipsedTimeSet){ elapsedTime = System.currentTimeMillis() - initialTime; if (elapsedTime >elipsedTimeSet){ WaterMelon wm = new WaterMelon(); addObject(wm, 5 , 330 ); elapsedTime = 0 ; initialTime = System.currentTimeMillis(); } } } |