Hello, I'm trying to make a counter. I need some help here.
This is the world's code:
This is the counter's code:
This is the spider's code:
I want to make it so everytime the spider eats an ant, the score goes up.
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 | public class Floor extends World { /** * Constructor for objects of class Floor. * */ public Floor() { super ( 600 , 400 , 1 ); } Counter counter = new Counter(); public Counter getCounter() { return counter; } private void prepare() { addObject(counter, 6 , 0 ); Spider spider = new Spider(); addObject(spider, 5 , 1 ); Spider spider2 = new Spider(); addObject(spider2, 1 , 1 ); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public class Counter extends Actor { private int totalCount = 0 ; public Counter() { setImage( new GreenfootImage( "0" , 20 ,Color.WHITE, Color.BLACK)); } public void bumpCount( int amount) { totalCount += amount; setImage( new GreenfootImage( "" + totalCount, 20 , Color.WHITE, Color.BLACK)); } } |
1 2 3 4 5 6 7 8 9 10 | Actor Ant; Ant = getOneObjectAtOffset( 0 , 0 ,Ant. class ); if (Ant != null ) { World world; world = getWorld(); world.removeObject(Ant); Floor floor = (Floor) getWorld(); Counter counter = floor.getCounter(); Counter.bumpCount( 1 ); |