Hey all :)
I have 2 classes that are the same: 'Persoon' and 'Animal'. They have the same code but the class 'Animal' gives me error.
CODE ANIMAL THIS IS THE CODE THAT GIVES ME AN ERROR. IT IS THE SAME AS PERSOON. (CODE BELOW)
----------------------------------------------------------------------------------------------------------
CODE PERSOON WORKS FINE!! IT IS THE SAME.
This is the error I get:
java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed.
at greenfoot.Actor.failIfNotInWorld(Actor.java:663)
at greenfoot.Actor.getOneIntersectingObject(Actor.java:912)
at Animal.followBoot(Animal.java:17)
at Dog.act(Dog.java:20)
at greenfoot.core.Simulation.actActor(Simulation.java:568)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:526)
at greenfoot.core.Simulation.runContent(Simulation.java:215)
at greenfoot.core.Simulation.run(Simulation.java:205)
java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed.
at greenfoot.Actor.failIfNotInWorld(Actor.java:663)
at greenfoot.Actor.getOneIntersectingObject(Actor.java:912)
at Animal.followBoot(Animal.java:17)
at Cat.act(Cat.java:19)
at greenfoot.core.Simulation.actActor(Simulation.java:568)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:526)
at greenfoot.core.Simulation.runContent(Simulation.java:215)
at greenfoot.core.Simulation.run(Simulation.java:205)
java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed.
at greenfoot.Actor.failIfNotInWorld(Actor.java:663)
at greenfoot.Actor.getOneIntersectingObject(Actor.java:912)
at Animal.followBoot(Animal.java:17)
at Pig.act(Pig.java:19)
at greenfoot.core.Simulation.actActor(Simulation.java:568)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:526)
at greenfoot.core.Simulation.runContent(Simulation.java:215)
at greenfoot.core.Simulation.run(Simulation.java:205)
----------------------------------------------------------------------------------------------------------
Do I have to change the image??
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Animal extends Actor
{
public int weight;
private int tempX;
private int tempY;
public void act()
{
removeObjectAnimal();
mouseInfo();
followBoot();
}
public void followBoot()
{
if(getOneIntersectingObject(Boat.class)!= null)
{
if (Greenfoot.isKeyDown("left") && getX() > 303)
{
move(-5);
}
if (Greenfoot.isKeyDown("right") && getX() < 772)
{
move(5);
}
}
}
public void removeObjectAnimal()
{
if (getX()>=820)
{
getWorld().removeObject(this);
}
}
public int getWeight()
{
return weight;
}
public void mouseInfo()
{
MouseInfo mouse = Greenfoot.getMouseInfo();
{
if (mouse == null)
{
return;
}
if(Greenfoot.mousePressed(this))
{
tempX = getX() - mouse.getX();
tempY = getY() - mouse.getY();
}
if(Greenfoot.mouseDragged(this))
{
setLocation(mouse.getX()+tempX,mouse.getY()+tempY);
}
}
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.Collection;
public class Persoon extends Actor
{
public int weight;
private int tempX;
private int tempY;
public void act()
{
removeObjectPersoon();
mouseInfo();
followBoot();
}
public void followBoot()
{
if(getOneIntersectingObject(Boat.class)!= null)
{
if (Greenfoot.isKeyDown("left") && getX() > 303)
{
move(-5);
}
if (Greenfoot.isKeyDown("right") && getX() < 772)
{
move(5);
}
}
}
public void removeObjectPersoon()
{
if (getX()>=820)
{
getWorld().removeObject(this);
}
}
public int getWeight()
{
return weight;
}
public void mouseInfo()
{
MouseInfo mouse = Greenfoot.getMouseInfo();
{
if (mouse == null)
{
return;
}
if(Greenfoot.mousePressed(this))
{
tempX = getX() - mouse.getX();
tempY = getY() - mouse.getY();
}
if(Greenfoot.mouseDragged(this))
{
setLocation(mouse.getX()+tempX,mouse.getY()+tempY);
}
}
}
}

