Hi I need help.
I have random Obstacle in my world but I want it to change location after a certain time what should I do?
// add instance field int counter; // add to 'act' method // or call a method with the following code from the 'act' method counter = (counter+1)%600; // adjust the 600 to suit (approx 10 secs) if (counter == 0) setLocation(Greenfoot.getRandomNubmer(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight());
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
* Write a description of class Food here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Food extends Actor
{
int counter;
/**
* Act - do whatever the Food wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public Food()
{
GreenfootImage image;
image = new GreenfootImage(20, 20);
image.setColor(Color.RED);
image.fillRect(0, 0, image.getWidth(), image.getHeight());
setImage(image);
}
public void act()
{
MyWorld world = (MyWorld)getWorld();
counter = (counter +1)% 600;
if( counter == 0)
{
setLocation(Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight()));
}
}
}