i sometimes have this error and find i need to save the scenario, close greenfoot and re open it all.
I have done this a lot of times!!!! no changes!!
damn that is odd, does it do it on other scenarios? download one from the greenfoot site and try it
Hey i downloaded one scenario from greenfoot and it ran!!!! What could be the problem??
Please post your world class code (the one that initially loads in your project).
This the first world:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class CrabWorld here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class CrabWorld1 extends World
{
/**
* Constructor for objects of class CrabWorld.
*
*/
public CrabWorld1()
{
super(560 , 560 , 1);
prepare();
}
/**
* Prepare the world for the start of the program. That is: create the initial
* objects and add them to the world.
*/
private void prepare()
{
Crab crab = new Crab();
addObject(crab, 122, 259);
Worm worm = new Worm();
addObject(worm, 68, 59);
Worm worm2 = new Worm();
addObject(worm2, 73, 494);
Worm worm3 = new Worm();
addObject(worm3, 480, 256);
Worm worm4 = new Worm();
addObject(worm4, 315, 259);
Worm worm5 = new Worm();
addObject(worm5, 293, 115);
Worm worm6 = new Worm();
addObject(worm6, 318, 399);
Lobster lobster = new Lobster();
addObject(lobster, 185, 139);
Crab crab2 = new Crab();
addObject(crab2, 212, 427);
removeObject(crab2);
worm6.setLocation(491, 502);
worm5.setLocation(466, 70);
worm4.setLocation(274, 261);
removeObject(worm3);
worm5.setLocation(371, 70);
worm6.setLocation(414, 498);
worm6.setLocation(383, 498);
lobster.setLocation(202, 117);
crab.setLocation(115, 360);
lobster.setLocation(110, 161);
worm2.getRotation();
worm2.move(5);
worm2.setLocation(67, 494);
worm2.act();
worm2.act();
worm2.setLocation(66, 496);
worm2.getX();
}
}
This is the second world:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class CrabWorld1 here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class CrabWorld2 extends World
{
/**
* Constructor for objects of class CrabWorld1.
*
*/
public CrabWorld2()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(560, 560, 1);
prepare();
}
/**
* Prepare the world for the start of the program. That is: create the initial
* objects and add them to the world.
*/
private void prepare()
{
Crab crab3 = new Crab();
addObject(crab3, 100, 129);
Lobster lobster2 = new Lobster();
addObject(lobster2, 161, 410);
Worm worm7 = new Worm();
addObject(worm7, 453, 123);
Worm worm8 = new Worm();
addObject(worm8, 248, 412);
Worm worm9 = new Worm();
addObject(worm9, 65, 317);
Worm worm10 = new Worm();
addObject(worm10, 253, 43);
Worm worm11 = new Worm();
addObject(worm11, 419, 513);
Worm worm12 = new Worm();
addObject(worm12, 315, 252);
}
}
i uploaded this project on greenfoot website....and now downloaded it nd now its working!!!
Actor crab:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.*;
public class Crab extends Actor
{
int ct=5,c=5;
/**
* Act - do whatever the Crab wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
moveturn();
eat();
ccounter();
}
public void ccounter()
{
if(ct==0)
{
ct=6;
c=6;
Greenfoot.setWorld(new CrabWorld2());
}
}
public void moveturn()
{
if(Greenfoot.isKeyDown("right"))
{
setLocation(getX()+3,getY());
}
if(Greenfoot.isKeyDown("left"))
{
setLocation(getX()-3,getY());
}
if(Greenfoot.isKeyDown("up"))
{
setLocation(getX(),getY()-3);
}
if(Greenfoot.isKeyDown("down"))
{
setLocation(getX(),getY()+3);
}
if(getX()==0)
{
setLocation(getWorld().getWidth()-2,getY());
}
if(getY()==0)
{
setLocation(getX(),getWorld().getHeight()-2);
}
if(getX()==getWorld().getWidth()-1)
{
setLocation(1,getY());
}
if(getY()==getWorld().getHeight()-1)
{
setLocation(getX(),1);
}
}
public void eat()
{
Actor worm;
worm = getOneObjectAtOffset(0,0,Worm.class);
if(worm != null)
{
World world;
world = getWorld();
world.removeObject(worm);
--ct;
Greenfoot.playSound("eating.wav");
}
}
}
Actor lobster:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Lobster here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Lobster extends Actor
{
/**
* Act - do whatever the Lobster wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
movearound();
eat();
}
public void movearound()
{
move(3);
if(Greenfoot.getRandomNumber(100)<10)
{
turn(Greenfoot.getRandomNumber(90)-45);
}
if(getX()==0)
{
setLocation(getWorld().getWidth()-2,getY());
}
if(getY()==0)
{
setLocation(getX(),getWorld().getHeight()-2);
}
if(getX()==getWorld().getWidth()-1)
{
setLocation(1,getY());
}
if(getY()==getWorld().getHeight()-1)
{
setLocation(getX(),1);
}
}
public void eat()
{
Actor crab;
crab = getOneObjectAtOffset(0,0,Crab.class);
if(crab != null)
{
World world;
world = getWorld();
world.removeObject(crab);
Greenfoot.playSound("eating.wav");
Greenfoot.setWorld(new Game());
Greenfoot.playSound("buzz.wav");
}
}
}
Actor worm:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Worm here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Worm extends Actor
{
/**
* Act - do whatever the Worm wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
move();
int cnt=counter();
if(cnt==6)
{
getWorld().repaint();
}
}
public void move()
{
move(1);
if(getX()==0)
{
setLocation(getWorld().getWidth()-2,getY());
}
if(getX()==getWorld().getWidth()-1)
{
setLocation(1,getY());
}
}
public int counter()
{
int c=5;
return(c++);
}
}
(new) World Game:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Game here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Game extends World
{
/**
* Constructor for objects of class Game.
*
*/
public Game()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(600, 400, 1);
}
}
Now i want to ask you something.....whenever it goes to crabworld2...after eating 5 worms the game ends!!!
Can you plz help me in finding the bug???
In your 'ccounter' method of the CrabWorld2 class, you are setting both 'c' and 'ct' to '6' and then starting a new world. It makes no sense to set these values in this world, as this world is destined for garbage collection. If you need those values to carry on through to the next world you would have to make those fields 'static' so that they are not locked onto any particular world object.