If the Button4 is not in a FarmWorld world when the code is executed, but in a BuyPlants world, then getWorld() can't be casted to a FarmWorld, it could be casted to a BuyPlants world. Is the counter not in the same world as the Button?
public class Board1 extends World
{
private Counter theCounter;
/**
* Constructor for objects of class Board1.
*
*/
public Board1()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(595, 535, 1);
addObject(new List2(), 295, 190);
addObject(new List3(), 295, 310);
addObject(new List4(), 295, 435);
showText("====GARDEN===", 295, 90);
showText("Buy Plants", 250, 190);
showText("Sell Plants", 250, 310);
showText("Look Plants", 250, 435);
if(Greenfoot.mouseClicked(List2.class)){
Change();
}
prepare();
}
public void Change(){
Counter counter = ((FarmWorld)getWorld()).getCounter();
buyPlants b1 = new buyPlants();
b1.getCounter().walkCount(counter.getValue());
Greenfoot.setWorld(b1);
}
public Counter getCounter(){
return theCounter;
}
}
Counter counter = ((FarmWorld)getWorld()).getCounter();
if(isTouching(Goa1.class)){
getWorldOfType(GoldWorld.class).stopMusic();
Counter counter = ((GoldWorld)getWorld()).getCounter();
FarmWorld fw = new FarmWorld();
fw.getCounter().walkCount(counter.getValue());
Greenfoot.setWorld(fw);
}
public class BuyPlants extends World
{
public buyPlants()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(595, 535, 1);
theCounter = new Counter();
addObject(theCounter, 50, 20);
showText("====BUY PLANTS====", 300, 70);
showText("X 60 GOLD", 290, 162);
showText("X 30 GOLD", 290, 287);
showText("X 20 GOLD", 290, 408);
showText("Buy", 450,162);
showText("Buy", 450,282);
showText("Buy", 450,400);
drawMap();
prepare();
}
public Counter getCounter(){
return theCounter;
}
public class Button4 extends Button
{
/**
* Act - do whatever the Button4 wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// Add your action code here.
if(Greenfoot.mouseClicked(this)){
Counter counter = ((BuyPlants)getWorld()).getCounter();
if (counter.getValue() >= 100)
{
counter.walkCount(-100);
}
}
}
public static void name (String[] args) throws IOException {
String Name;
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
System.out.print("Input yoour Grape's plant name: ");
Name = br.readLine();
System.out.println("Your Plants Name : " + Name);
}
}
Counter counter = ((Board1)getWorld()).getCounter();
Counter counter = ((BuyPlants)getWorld()).getCounter();
public class Board1 extends World
{
private Counter theCounter;
/**
* Constructor for objects of class Board1.
*
*/
public Board1()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(595, 535, 1);
addObject(new List2(), 295, 190);
addObject(new List3(), 295, 310);
addObject(new List4(), 295, 435);
showText("====GARDEN===", 295, 90);
showText("Buy", 250, 190);
showText("Sell", 250, 310);
showText("Look", 250, 435);
prepare();
}
public void act(){
if(Greenfoot.mouseClicked(List2.class)){
Change();
}
}
public void Change(){
Counter counter = ((FarmWorld)getWorld()).getCounter();
BuyPlants b1 = new BuyPlants();
b1.getCounter().walkCount(counter.getValue());
Greenfoot.setWorld(b1);
}
public Counter getCounter(){
return theCounter;
}
addObject(theCounter = new Counter(), <x>, <y>);
public void changeWorld()
{
BuyPlants b1 = new BuyPlants();
b1.getCounter().walkCount(theCounter.getValue());
Greenfoot.setWorld(b1);
}
if(isTouching(Goa1.class)){
getWorldOfType(GoldWorld.class).stopMusic();
Counter counter = ((GoldWorld)getWorld()).getCounter();
FarmWorld fw = new FarmWorld();
fw.getCounter().walkCount(counter.getValue());
Greenfoot.setWorld(fw);
}