Im trying to make so a enemy I appear follows the player character, but when I try to get the location of the player so the enemy can turn towards the player and subsequently move towards him, it gives a null pointer.
I have searched for answers in the forum, but when I implement them It just doesnt work. I still dont know why.
This is my code right now, but I have changed it repeatedly and doesnt work.
The main code that spawns the enemy are this:
This is the error:
java.lang.NullPointerException
at Enemigos.mirarAlUsuario(Enemigos.java:29)
at Enemigos.<init>(Enemigos.java:17)
at MaleanteComun.<init>(MaleanteComun.java:14)
at MyWorld.anadirEnemigos(MyWorld.java:27)
at MyWorld.act(MyWorld.java:20)
at greenfoot.core.Simulation.actWorld(Simulation.java:573)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:506)
at greenfoot.core.Simulation.runContent(Simulation.java:193)
at greenfoot.core.Simulation.run(Simulation.java:183)
protected void lookAtUser() { Actor usuario= (Actor)myWorld.getUsuario(); turnTowards(usuario.getX(), usuario.getY()); }
private void anadirEnemigos() { if(Greenfoot.getRandomNumber(100)<1) { addObject(new MaleanteComun(), 400, 1); } }