i have a money counter which is in one of the actors, how can i get another actor also add to the same counter


1 2 3 | // This is in ActorA static int money_counter; |
1 | ActorA.money_counter = 0 ; |
1 | ActorA.money_counter = ActorA.money_counter + 5 ; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import greenfoot.*; import java.awt.Color; public class Counter extends Actor { int value = 0 ; public Counter() { updateImage(); } public void add( int amount) { value += amount; updateImage(); } public int getValue() { return value; } private void updateImage() { setImage( new GreenfootImage( "Score: " + value, 24 , Color.black, new Color( 0 , 0 , 0 , 0 ))); } } |
1 2 3 4 5 6 7 8 9 | // in the actor class act method (or method it calls) if (whatever == true ) addToCounter( 10 ); // and add the following method private void addToCounter( int addAmount) { WorldName world = (WorldName) getWorld(); Counter counter = (Counter) world.getObjects(Counter. class ).get( 0 ); counter.add(addAmount); } |