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:
/**
* 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;
}/**
* 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));
} /**
* 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;
}
}
