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

2014/5/7

Problems with changing worlds.

DennisDvD DennisDvD

2014/5/7

#
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:
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;
            }
        
    }
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()
    {
            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;
                }
            }       
    }
danpost danpost

2014/5/7

#
You do not need the int variable to do the check to see what world you are in. You can use the 'instanceof' keyword to determine which class the world is.
if (getWorld() instanceof Main))
danpost danpost

2014/5/7

#
My main question to you is: why are the car variables not in the Car class?
DennisDvD DennisDvD

2014/5/7

#
Thanks, just a question, how does "instanceof" work? I can't find an understandable explanation of it anywhere, could you explain it in words what it does please?
DennisDvD DennisDvD

2014/5/7

#
Also, the variables are in the Car class, but I need to use a boolean in that class in this class.
danpost danpost

2014/5/7

#
danpost wrote...
use the 'instanceof' keyword to determine which class the world is.
Change the words 'the world' to 'an object' for a more complete definition. Please refer to the last section of this page of the Java tutorials.
DennisDvD DennisDvD

2014/5/7

#
Thanks Dan, always a saviour
You need to login to post a reply.