Does anyone know what I need to do in order top get the counters to work so that they count a lap every time the car passes the finish line?
Here is the code form one of the counters:
Here is the code from one of the cars:
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 29 30 31 32 33 34 35 | /** * The track. * * @author (Matthew St. George) * @version (1.0) */ public class Track extends World { private Counter theCounter; private Counter2 theCounter2; /** * Constructor for objects of class MyWorld. * */ public Track() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super ( 800 , 540 , 1 ); theCounter = new Counter(); addObject(theCounter, 40 , 500 ); theCounter2 = new Counter2(); addObject(theCounter2, 50 , 450 ); prepare(); } public Counter getCounter() { return theCounter; } public Counter2 getCounter2() { return theCounter2; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | /** * Write a description of class Counter here. * * @author (your name) * @version (a version number or a date) */ public class Counter extends Actor { /** * Act - do whatever the Counter wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ private int totalCount = 0 ; private void Counter() { setImage( new GreenfootImage( "0" , 20 , greenfoot.Color.WHITE, greenfoot.Color.BLACK)); } public void bumpCount( int amount) { totalCount += amount; setImage( new GreenfootImage( "" + totalCount, 20 , greenfoot.Color.WHITE, greenfoot.Color.BLACK)); } |
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | /** * Write a descriptiocomman of class Car here. * * @author (your name) * @version (a version number or a date) */ public class Car extends Actor { public double moveVal; public Car() { moveVal = 0 ; } /** * Act - do whatever the Car wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. Delete else * commands, in the act method, make it so if speed doesn't equal 4, then * increase speed by ex:0.2 */ double speed = 4 ; double acceleration = 0.5 ; public void act() { if ( Greenfoot.isKeyDown( "left" ) ) { turn(- 5 ); } if ( Greenfoot.isKeyDown( "right" ) ) { turn( 5 ); } move(( int )moveVal); if ( Greenfoot.isKeyDown( "up" ) && moveVal < 4 ) { moveVal+= 0.2 ; } if ( Greenfoot.isKeyDown( "down" ) && moveVal > 0 ) { moveVal-= 0.2 ; } int x = getX(), y = getY(); if (isTouching(Wall. class )) { setLocation( 327 , 485 ); } checkSlow(); } public void accelerate() { if (Greenfoot.isKeyDown( "up" )) speed += acceleration; else speed -= acceleration; move(( int )speed); if (speed > 7 ) speed = 7 ; else if (speed <= 0 ) speed = 0 ; } public void checkSlow() { if (isTouching(Grass. class )) { speed= 2 ; } else { speed= 4 ; } if (isTouching(Oil_Spot. class )) { speed= 2 ; } else { speed= 4 ; } } |