Hi,
So I am working through the Greenfoot book in Chapter 4.
I want to make it so worm appear randomly.
So I put the following code in the Worm class.
However, I get the error - non-static method addObject cannot be referenced from static contex.
I have searched this issue and from what I understand the addObject is an instance method and therefore cannot be called from a class.
I considered moving it into the Crabworld class but as there is no act method I couldnt see how it would run.
So I tried to create the worm first and then add it.
but still no luck, any help would be appreciated.
public void act()
{
makeRandomWorm();
}
public void makeRandomWorm(){
int x = 0;
if(x < 10)
{
CrabWorld.addObject(new Worm(), Greenfoot.getRandomNumber(560), Greenfoot.getRandomNumber(560));
x = x + Greenfoot.getRandomNumber(600);
}
} public void act()
{
makeRandomWorm();
}
public void makeRandomWorm(){
int x = 0;
if(x < 10)
{
Worm a = new Worm();
Greenfoot.addObject(Worm a, 100, 100);
x = x + Greenfoot.getRandomNumber(600);
}
}
