Hi all, I'm making a code that when my character moves to the side of the screen it changes to a new world. Everything works as expected but there is one problem, I have the following code for grabbing variables from other classes:
When I change worlds, I get the error "java.lang.ClassCastException: City cannot be cast to Main". I understand what this means, so I figured I'd add an integer which keeps track of which world it is in, and it does a check, but I still get the error with the following code, any help?
public void moveCar()
{
Main mainWorld = (Main) getWorld();
Car carVars = mainWorld.getCarVariables();
Actor car = getOneIntersectingObject(Car.class);
if(car != null && Greenfoot.isKeyDown("f"))
{
inCar = true;
invisible = true;
setImage(empty);
}
if(inCar && Greenfoot.isKeyDown("g"))
{
setImage(walk1);
setLocation(carVars.getX() + 75, carVars.getY() + 75);
invisible = false;
inCar = false;
}
}public void moveCar()
{
if ( level == 1 )
{
Main mainWorld = (Main) getWorld();
Car carVars = mainWorld.getCarVariables();
Actor car = getOneIntersectingObject(Car.class);
if(car != null && Greenfoot.isKeyDown("f"))
{
inCar = true;
invisible = true;
setImage(empty);
}
if(inCar && Greenfoot.isKeyDown("g"))
{
setImage(walk1);
setLocation(carVars.getX() + 75, carVars.getY() + 75);
invisible = false;
inCar = false;
}
}
}
