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

2019/7/7

getWorld variable

liverpoolfc liverpoolfc

2019/7/7

#
how to use the getWorld variable? When I use it says (cannot find symbol - variable getWorld)
Super_Hippo Super_Hippo

2019/7/7

#
It is not a variable, it is a method. You have to use getWorld(), for example:
getWorld().addObject(new Bullet(), getX(), getY()); //add an instance of class Bullet at the current location of the actor
liverpoolfc liverpoolfc

2019/7/7

#
public class dolphin extends Actor
{
    private final int gravity = 1;
    private int velocity;
    public dolphin() {
        velocity = 0;
    }
    public void act() 
    {
        fall();
        if(Greenfoot.isKeyDown("space") && getY() > getWorld.getHeight - 50)jump();
        move();
    }    
    public void fall() {
        setLocation(getX(),getY() + velocity);
        if (getY() > getWorld.getHeight - 50)velocity = 0;
        else  velocity += gravity;
    }
    public void jump()
    {
        velocity = -20;
    }
    public void move() {
        int y= getY();
        int x= getX();
        if(Greenfoot.isKeyDown("left"))x--;
        if(Greenfoot.isKeyDown("right"))x++;
        setLocation(x,y);
    }
}
my program is like this but when I compile it says cannot find variable getWorld().
Super_Hippo Super_Hippo

2019/7/7

#
It will say "cannot find variable getWorld", because you missed the ( and ).
liverpoolfc liverpoolfc

2019/7/7

#
and? what and?
Super_Hippo Super_Hippo

2019/7/7

#
For example instead of
getWorld.getHeight
you need to use
getWorld().getHeight()
liverpoolfc liverpoolfc

2019/7/7

#
it works. thanks a lot.
You need to login to post a reply.