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

2015/3/7

how to make a car actor stop when the light actor sets its image to red light

1
2
Rayray Rayray

2015/3/7

#
i have got the car to stop at a certain point on the world however i want it now to stop when it reaches a certain point and the actor traffic light has a red light image set thanks!!
danpost danpost

2015/3/7

#
If you traffic light actor is large enough, using 'getOneObjectAtOffset' to look for it might be enough. If not, you may want to add a 'sensor' actor for each traffic light actor, strategically positioned so the car can use 'getOneIntersectingActor' to locate a sensor. Since there will be one sensor per traffic light, each one can hold a reference to the traffic light it is placed for. This will allow the car to determine the state of the light through the sensor.
Rayray Rayray

2015/3/7

#
thanks would this work if i wanted to get the car to move again when the light goes green again?
Rayray Rayray

2015/3/7

#
and how would i add a reference to the sensor class?
danpost danpost

2015/3/7

#
Rayray wrote...
thanks would this work if i wanted to get the car to move again when the light goes green again?
Absolutely.
Rayray wrote...
and how would i add a reference to the sensor class?
A basic Sensor class might be:
import greenfoot.*;

public class Sensor extends Actor
{
    private TrafficLight trafficLight;

    public Sensor(TrafficLight light)
    {
        trafficLight = light;
        setImage(new GreenfootImage(60, 10)); // adjust as needed
    }

    public TrafficLight getTrafficLight()
    {
        return trafficLight;
    }
}
Then, your car can use:
Sensor sensor = (Sensor)getOneIntersectingObject(Sensor.class);
if (sensor == null || sensor.getTrafficLight().getValue() == /** etc */ )
I cannot complete the last line as I do not know what your TrafficLight class looks like. Basically, what it would be saying is that if there is no sensor or the light the sensor is there for is GREEN, then move. (just a note: you cannot really tell it to stop; you just do not tell it to move)
Rayray Rayray

2015/3/7

#
in my traffic light class i have it on timer like so it goes on to green and then back to red etc ...
if(timer >= 0 && timer < 200)
        {
            traffic = 0;
            setImage(red);
            System.out.println("red");
            timer ++;
        }
        if(timer >= 200 && timer < 400)
        {
            traffic = 1;
            setImage(amber);
            System.out.println("amber");
            timer ++;
        }
im confused on what to put in the if statement in car class regarding the getValue could i put traffic interger or the actual image variable
danpost danpost

2015/3/7

#
Please post the fields and the constructor methods for the traffic light class. I am particularly interested in how 'red' and 'amber' are declared and in the initial setting of the image of the actor. Or, maybe you have a 'getTraffic' getter method for the 'traffic' field.
Rayray Rayray

2015/3/7

#
private GreenfootImage green;
    private GreenfootImage amber;
    private GreenfootImage red;
private int traffic; 
    private int timer = 0;
green = new GreenfootImage ("button-green.png");
        amber = new GreenfootImage ("button-amber.png");
        red = new GreenfootImage ("button-red.png");
and yes i do have a getTraffic method in traffic light
Rayray Rayray

2015/3/7

#
the previous if statement is in the act method for traffic light
danpost danpost

2015/3/7

#
Rayray wrote...
and yes i do have a getTraffic method in traffic light
Very well, instead of 'getValue', use 'getTraffic' and check its value to be equal to one (1) in my code above.
Rayray Rayray

2015/3/7

#
is it == or .equals as the car still stops at the sensor?
danpost danpost

2015/3/7

#
Rayray wrote...
is it == or .equals as the car still stops at the sensor?
It should be:
if (sensor == null || sensor.getTrafficLight().getTraffic() == 1) // move
If the car stops at the sensor and you think the light the sensor is there for is green, then do a bit of 'Inspect'ing. Right-click on Sensor object the car is at; select 'Inspect'. Click on TrafficLight field and click on 'Inspect' button. Look at two things here -- the value of the 'traffic' field (which should be '0' if the car stopped) and the x and y location coordinates; right-click on the traffic light actor you believe the sensor was for and compare the coordinates -- they should match.
danpost danpost

2015/3/7

#
Also, make sure you remove code placed previously in your car class to stop the car (you originally said you were able to have the car stop -- maybe that code is stopping it still).
Rayray Rayray

2015/3/7

#
there seems to be a problem the sensor coordinates and traffic vairable which is 0 while the actual light class is different
danpost danpost

2015/3/7

#
Please show your world constructor and prepare methods.
There are more replies on the next page.
1
2