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

2020/5/7

"Non-static method 'getY()' cannot be referenced from a static context"

yeeterboi420 yeeterboi420

2020/5/7

#
The class I'm trying to use it in (Block.class):

public class Block extends Actor
{
    int y = Player.getY();
    public Block()
    {
        getImage().scale(20, 20);
    }
    public void act()
    {
        if(isTouching(Block.class))
        {
            removeTouching(Block.class);
        }
    }
    public boolean checkPlayer()
    {
        if(y == this.getY()-20) return true;
        return false;
    }
}
Can anyone help me?
Super_Hippo Super_Hippo

2020/5/7

#
Remove line 5. First, this line is executed once when the Block object is created. It doesn’t change when the player moves. Second, a class doesn’t have a location. An object does. In your case, maybe the following could work:
public boolean checkPlayer()
{
    return getOneObjectAtOffset(0, -20, Player.class) != null;
}
If you want the condition to be independent on the x-coordinate, you need to get a reference to the Player in the world:
public boolean checkPlayer()
{
    return getY()-20 == ((Player) (getWorld().getObjects(Player.class).get(0)).getY();
}
If there is any chance that there is not exactly one player object in the world (or the block isn’t in a world), you need to check for null before calling methods on the returned objects. By the way: Are your Blocks supposed to remove Blocks?
You need to login to post a reply.