This site requires JavaScript, please enable it in your browser!
Greenfoot back
Saswat
Saswat wrote ...

2019/5/2

Lap counter continues to count

Saswat Saswat

2019/5/2

#
I tried to create a counter which counts the lap(s) completed by a car but whenever the car touches the finish line the counter counts continuously..how can can i call the method of another actor once at a time so that the counter works properly...
danpost danpost

2019/5/2

#
Saswat wrote...
I tried to create a counter which counts the lap(s) completed by a car but whenever the car touches the finish line the counter counts continuously..how can can i call the method of another actor once at a time so that the counter works properly...
Add a boolean field to track the state of the car touching the line:
private boolean touchingLine;

// in act
if (touchingLine != isTouching(Line.class)) // if change in state
{
    touchingLine = !touchingLine; // track new state
    if (touchingLine) laps++; // if new state is true, add to counter
}
Adding a boolean like this allows you to detect when the change occurs as opposed to just getting the current state between the changes.
Saswat Saswat

2019/5/3

#
Thank you..it worked...
You need to login to post a reply.