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

2015/3/31

I just want to stop the car while the traffic light is Yellow, And should move the car when the traffic light is Green.

VIvek_Rajagopal VIvek_Rajagopal

2015/3/31

#
import greenfoot.*;
import java.util.List;
import java.lang.Object;

public class MercSLR extends SmoothMover
{
    
    public void act() 
    {
        
       lightSensor();
       
    }   
    
    public void lightSensor()
    {
     
        move(1);
        Actor a = getOneIntersectingObject(SignalOne.class);
         
        GreenfootImage sensor = new GreenfootImage("wyellow.png");
       
        if(getImage() == sensor)
        {
           pause();
        }
    }
}
VIvek_Rajagopal VIvek_Rajagopal

2015/3/31

#
Actually, the traffic signal is place in the location of (550,700). It runs on a timer basis. // NO PROBLEM in Traffic Signal For Yellow it has "wyellow.png" and for Green is has "wgreen.png" My doubt is, 1. How to I move the car when the traffic light gets changed from Green to Yellow ? 2. How to I move the car when the traffic light gets changed from Yellow to Green ?
danpost danpost

2015/3/31

#
(1) Line 2 can be removed as no lists are being referenced in the class; (2) Line 3 can be removed because the java.lang.Object class is automatically available to all classes; (3) Line 19 declares a variable that is not later used within the method; it can be removed without changing the way the method currently executes (I am not saying to actually remove it, here); (4) Line 23 compares a new object to a previously created object; they are two distinct objects; that is, they are not the same object (however similar they may be) and the comparison will show that they are not the same object (always returning a 'false' result); you may be able to make the comparison with something like, for example:
if (a.getImage().toString().contains("wgreen.png"))
(5) Line 25 calls a 'pause' method; I do not see that method anywhere within this class (nor did I see it in the SmoothMover class);
You need to login to post a reply.