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

2016/10/26

getting an tire cannot be cast to Car error message plz help

hockeydude1155 hockeydude1155

2016/10/26

#
    public void act() 
    {
       
       if(Greenfoot.isKeyDown("w"))
       {
            gameStarted = true;
       } 
        if (gameStarted == true)
        {
            move (6);
       }
       if (Greenfoot.isKeyDown("a"))turn(-6);
       Car a = (Car) getOneIntersectingObject(tire.class);
    if(a != null) {
        setLocation(213,441);
        setRotation(0);
        return;
    }    
}
hockeydude1155 hockeydude1155

2016/10/26

#
all I'm trying to do is if the car hits the tire it goes back to the starting point
danpost danpost

2016/10/26

#
hockeydude1155 wrote...
all I'm trying to do is if the car hits the tire it goes back to the starting point
Just change line 13 to:
Actor a = getOneIntersectingObject(tire.class);
If you really needed the reference specifically as a tire object (which you do not), you would then use:
tire a = (tire)getOneIntersectingObject(tire.class);
where a tire object (or '(tire)null') is set to a variable that accepts a tire object (not a Car object).
Nosson1459 Nosson1459

2016/10/26

#
use
if(isTouching(Tire.class));
instead of lines 13 and 14
danpost danpost

2016/10/26

#
Nosson1459 wrote...
use
if(isTouching(Tire.class));
instead of lines 13 and 14
If you make this change, it should be:
if (isTouching(tire.class)) {
It is customary to have class names begin with an uppercase letter, which is probably why Nosson1459 made it so. But, the semi-colon at the end makes the 'if' statement all for naught. It, the semi-colon, is equivalent to '{ }', an empty block of code. The following statements will execute regardless of the result of the condition of the 'if' statement.
Nosson1459 Nosson1459

2016/10/26

#
my bad stupid mistake not wat I meant though
You need to login to post a reply.