Do you ever use "getWorld" when creating a Counter and adding it to the world?
Counter counter = ((FarmWorld)getWorld()).getCounter();
(FarmWorld) getWorld()
Counter counter = ((FarmWorld)getWorld()).getCounter();
public void act(){
if(Greenfoot.mouseClicked(List2.class)){
Change();
}
}
public void Change(){
Counter counter = new Counter();
BuyPlants b1 = new BuyPlants();
b1.getCounter().walkCount(counter.getValue());
Greenfoot.setWorld(b1);
}
public class Counter extends Actor
{
private int count = 0;
public Counter(){
setImage(new GreenfootImage("Gold : 100", 20, Color.WHITE, Color.BLACK));
}
/**
* Act - do whatever the Counter wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void walkCount (int amount)
{
// Add your action code here.
count += amount;
setImage(new GreenfootImage("Gold : " + (100+count),20, Color.WHITE, Color.BLACK));
}
public int getValue(){
return count;
}
}
//outside methods
private List2 list2 = new List2();
//…
addObject(list2, 295, 190);
//…
if (Greenfoot.mousePressed(list2))
{
Change();
}public void Change()
{
BuyPlants b1 = new BuyPlants();
b1.getCounter().walkCount(theCounter.getValue());
Greenfoot.setWorld(b1);
}