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

2021/3/27

Changing an Actors speed when not on Track

hernry2812 hernry2812

2021/3/27

#
Im working on a Car Game where the Car should get slowed down when its not on the race track anymore. My Car and the race track are both actors, thats why I tried out the following code (car class):
public class Car extends SmoothMover
{
    double speed = 0;
    double maxSpeed = -4;
    double acceleration = -0.02;
    double breaking = 0.008;
    
    public void act() 
    {
        driving();
        checkOnTrack();
        
    }
    
    public void driving()
    {
        move(speed);
        
        if(Greenfoot.isKeyDown("a"))
        {
            turn(-2);
        }
        
        if(Greenfoot.isKeyDown("d"))
        {
            turn(2);
        }
        
        if(Greenfoot.isKeyDown("w") && speed > maxSpeed)
        {
            speed = speed + acceleration;
        }
        
        if(!Greenfoot.isKeyDown("w") && speed < 0)
        {
            speed = speed + breaking;
        }
    }
    
    public void checkOnTrack()
    {
        Actor Car = getOneIntersectingObject(RaceTrack.class);
        if(Car !=null)
        {
            double speed = 0;
            double maxSpeed = -4;
            double acceleration = -0.02;
            double breaking = 0.008;
        }
        else
        {
            double maxSpeed = -2;
            double acceleration = -0.02;
            double breaking = 0.08;
        }
    }
    
    
}
This solution isn't working. My assumption was that transparent pixels count as an actor too. My Race Track Image is as big as my World image, but only the pixels of the track are not transparent, the rest is. Can someone tell me why this isn't working? Any help is appreciated ;)
danpost danpost

2021/3/27

#
All collision methods provided by greenfoot in the Actor class are implemented without regard to transparency. That is, if any part of the rectangular frame of one actor intersects another, then the actors are considered to be touching (or intersecting) each other.
hernry2812 hernry2812

2021/3/27

#
Have you got a possible solution for my case? How can I slow down the Car if it's not on the track? The shape of my track looks nearly like a closed circle
Super_Hippo Super_Hippo

2021/3/28

#
If the race track is the world class and the image is the background of the world, you could try to check the color of the pixels on the image where the car currently is and if X% of it is not the track, slow it down.
danpost danpost

2021/3/28

#
@Hippo, from an earlier discussion, I know for a fact that the race track is an actor. hernry2812 would need to determine what pixel of its image the car is at and determine slow down amount from its color.
hernry2812 hernry2812

2021/3/28

#
I now removed the race track from my actors and painted it onto my world background. No I tried to achieve the slowing of the car by tracking the color of the worlds image underneath it. The color of my race track is R=77, G=77, B=77 Hex.: 4D4D4D Thats the code for my car class now but for a reason it doesn't slow down the car:
public class Car extends SmoothMover
{
    double speed = 0;
    double maxSpeed = -4.5;
    double acceleration = -0.02;
    double autobreaking = 0.008;
    double manualbreaking = 0.03;
    Color color;

    public void act() 
    {
        driving();
        checkOnTrack();

    }

    public void driving()
    {
        move(speed);

        if(Greenfoot.isKeyDown("a"))
        {
            turn(-2);
        }

        if(Greenfoot.isKeyDown("d"))
        {
            turn(2);
        }

        if(Greenfoot.isKeyDown("w") && speed > maxSpeed)
        {
            speed = speed + acceleration;
        }

        if(Greenfoot.isKeyDown("s") && speed < 0)
        {
            speed = speed + manualbreaking;
        }

        if(!Greenfoot.isKeyDown("w") && speed < 0)
        {
            speed = speed + autobreaking;
        }

    }
    
    public void checkOnTrack()
    {
        Color color = getWorld().getColorAt(this.getX(), this.getY());
        if (color.getRed() == 77 && color.getGreen() == 77 && color.getBlue() == 77) 
        {
            double maxSpeed = -4.5;
            double acceleration = -0.02;
            double autobreaking = 0.008;
        }
        else
        {
            double maxSpeed = -2;
            double acceleration = -0.02;
            double autobreaking = 0.08;
        }
    }   
}
danpost danpost

2021/3/28

#
hernry2812 wrote...
for a reason it doesn't slow down the car:
One problem is that you are creating local double type variables in the checkOnTrack method. That is, they are NOT the same as the fields declared between lines 3 thru 7.
hernry2812 hernry2812

2021/3/28

#
Thanks, it works now^^
You need to login to post a reply.