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

2013/1/1

How do you make worms appear only some of the time?

1
2
danpost danpost

2013/1/1

#
OK, so if you already have a timer in the world that decrements, then add a red worm when the elapsed time (in seconds) is divisible by 18 evenly. Place the code for this immediately after decrementing the timer and before checking for an expired timer (time has run out). If your timer is controlled by the system clock, you will probably need some way to ensure that only one is added each 18 seconds (maybe set up a variable to be the target value at which to add a red worm, checking the timer with the target value; when adding a red worm, set a new target value).
LonelyCreeper LonelyCreeper

2013/1/2

#
How do I put that in code?
danpost danpost

2013/1/2

#
I would have to know how the timer works (whether it is a cycle counter or actually uses the system clock).
LonelyCreeper LonelyCreeper

2013/1/3

#
it's a countdown clock that counts down the seconds as soon as the games starts. here's the code if it's useful: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; import java.util.Calendar; /** * Displays a digital clock in HH:MM:SS format. */ public class CountdownClock extends Actor { private int width = 60; // Dimensions of the display private int height = 20; private int prevSecond = -1; private int counter = 180; /** * The constructor creates the actor's image. */ public CountdownClock() { GreenfootImage image = new GreenfootImage(width, height); image.setColor(Color.WHITE); image.fill(); image.setColor(Color.BLACK); image.drawRect(0, 0 ,width - 1, height - 1); setImage(image); updateDisplay(counter + ".0"); } /** * The clock is tick'd on every act() call, but the display changes * only when the second's increments. */ public void act() { if (counter > 0) { updateDisplay(tick()); } } /** * Increment the display string for the clock. */ public String tick() { // Compose new display string Calendar calendar = Calendar.getInstance(); int sec = calendar.get(Calendar.SECOND); int tsec = calendar.get(Calendar.MILLISECOND) / 100; if (counter == 0) { return("0.0"); } if (sec != prevSecond ) { prevSecond = sec; counter = counter - 1; } return(counter + "." + tsec); } /** * Update the clock with the new time string. */ public void updateDisplay(String newTime) { // x and y relative to the image. baseline of leftmost character. int x = 5; int y = 15; // Repaint the clock display GreenfootImage image = getImage(); image.setColor(Color.WHITE); image.fillRect(1, 1, width-2, height-2); // "erase" the display image.setColor(Color.BLACK); image.drawString(newTime, x, y); setImage(image); } /** * Returns the current value of the clock. */ public int getCounter() { return counter; } }
You need to login to post a reply.
1
2