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

2014/9/21

Problem with if(getWorld() == new World())

nivilom86 nivilom86

2014/9/21

#
Hello ! I've a problem that I do not manage to resolve : I try to create a "gravity" effect, but in only one world. I use this code for that :
public void gravity()
    {
        if(getWorld() == Water())
        {
            int gravite = getY() + 1;
            setLocation(getX(), gravite);
        }
    }
There is no syntax error, but in the world Water, the gravity doesn't work...
erdelf erdelf

2014/9/21

#
try it like this
if(getWorld() instanceOf Water)
you are calling a method called water in your code, i am surprised it compiles
nivilom86 nivilom86

2014/9/22

#
Thanks for your answer, but it doesn't work. There is a syntax error that say ")" expected after getWorld().
nivilom86 nivilom86

2014/9/22

#
    public void gravity()
    {
        if(getWorld() instanceOf Water)
        {
            int gravite = getY() + 1;
            setLocation(getX(), gravite);
        }
    }
The "(" expected is just after if(getWorld()
erdelf erdelf

2014/9/22

#
oh.. try it like this
if(getWorld() instanceof Water) 
nivilom86 nivilom86

2014/9/22

#
A last thing, sorry for the uncoveniance, but what is the difference between "instanceOf" and "==" ?
nivilom86 nivilom86

2014/9/22

#
It works, thank you so much ! ! !
erdelf erdelf

2014/9/22

#
instanceof checks if the object, here referenced with getWorld(), is an object of the class, but == would check if its equal, but an object and a class can never be equal
nivilom86 nivilom86

2014/9/22

#
Ok, thank you more and more ;)
You need to login to post a reply.