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

2019/4/26

Hello, can anyone help with this error pls?

Night Night

2019/4/26

#
i'm trying to track an object in the world and then turn towards it. There is this error that happens saying that I can't use the getX/Y(); function because its static. Is there a way to fix this?
import greenfoot.*;

/**
 * Write a description of class Predator here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Predator extends Actor
{
    /**
     * Act - do whatever the Predator wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        tracking();
    }
    
    public void tracking()
    {
        turnTowards(prey.getX(), prey.getY());
    }
}
danpost danpost

2019/4/26

#
The error is not saying that you have static content. Actually, it is saying that getX (and getY) are not static methods; but, you are trying to use them as if they were. You cannot use the class name to execute those methods. You need one of the actors created from the class (an instance of the class) to execute the method on. You should be able to get it with:
Actor p = (Actor)getWorld().getObjects(prey.class).get(0);
Then execute the methods on p.
Night Night

2019/4/27

#
thx :D
You need to login to post a reply.