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

2016/2/2

Lap counter problem

Andrei Andrei

2016/2/2

#
Hello everyone, I`ve got a problem with my racing game. The problem is I can't get the lap counter work properly and I really don`t know what to do as I`m a beginner.
public class LapLine extends Actor
{
    int S1Lap = 0;
    int S2Lap = 0;
    public void act() 
    {
        if(isTouching(Subaru.class))
      {
          S1Lap++;
          setImage(new GreenfootImage("S1Lap: " + S1Lap,24,Color.GREEN, Color.BLACK));
        }
        if(isTouching(Subaru2.class))
        {
            S2Lap++;
            setImage(new GreenfootImage("S2Lap: " + S2Lap,24,Color.GREEN, Color.BLACK));
        }        
}
    public boolean isTouching(Class clss)
        {
        
            Actor actor = getOneObjectAtOffset(5, 5, clss);
            return actor != null;        
        }  
}
The code below is from my car class:
Actor Subaru = getOneIntersectingObject(LapLine.class); if(isTouching(LapLine.class)) { World myWorld = getWorld(); Level1 level1 = (Level1) myWorld; S1Lap s1Lap = level1.getS1Lap(); s1Lap.adds1Lap(); } This is from my LapCounter class : public class S1Lap extends Subaru { int S1Lap = 0; public void act() { setImage(new GreenfootImage("S1Lap: " + S1Lap,24,Color.GREEN, Color.BLACK)); } public void adds1Lap() { S1Lap++; } }
danpost danpost

2016/2/2

#
Your class tree structure seems suspicious. Please list the classes in your scenario; give what class extends what and a brief description as to what each class is for.
Andrei Andrei

2016/2/2

#
I don`t really know how to explain it so I will just send you a picture link with the classes. Just to mention the S1Lap and S2Lap are the lap counter for each car. And also the LapLine is the class which the cars has to touch it in order to increase the lap counter. I don`t really know how well is it working so have a look: https://imgur.com/O9S8yIa
danpost danpost

2016/2/2

#
The lap counters are not vehicles and should not be subclassed under the Vehicle class.
You need to login to post a reply.