Hello.. Iam nearly done with my game, but i got this last problem. When my Obstacle.class hits the worldEdge, it crashes. I get this message:
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.getY(Actor.java:170)
at Obstacle.atWorldEdge(Obstacle.java:42)
at Obstacle.act(Obstacle.java:23)
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.getY(Actor.java:170)
at Obstacle.atWorldEdge(Obstacle.java:42)
at Obstacle.act(Obstacle.java:23)
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)
My whole code for my Obstacle class:
public class Obstacle extends Actor
{
private int vSpeed = 5; // vSpeed = 5
private Counter counter;
public Obstacle(Counter pointCounter)
{
counter = pointCounter;
}
public void act()
{
fall();
checkOut();
atWorldEdge();
checkEdge();
//spawn();
createObstacle();
}
/**
* Får X- og Y-koordinater + vSpeed
*/
public void fall()
{
setLocation ( getX(), getY() + vSpeed);
}
/**
* Boolean om Obstacle class er ved Y-koordinaten 499 (nederste væg)
*/
public boolean atWorldEdge()
{
if(getY() == 499)
return true;
else
return false;
}
/**
* Hvis boolean == true, så fjerner Obstacle class
*/
private void checkOut()
{
if (atWorldEdge() == true)
{
getWorld().removeObject(this);
}
}
/**
*
*/
public void checkEdge()
{
if (this.atWorldEdge() == true)
{
World world;
world = getWorld();
world.removeObject(this);
counter.add(1);
}
}
/**
*
*public void spawn()
{
int x = Greenfoot.getRandomNumber(getWorld().getWidth() - 1);
int y = 2;
if (Greenfoot.getRandomNumber(1000) >= 10)
{
Obstacle obstacle = new Obstacle(counter);
getWorld().addObject(new Obstacle(counter), x, y);
}
}*/
private void createObstacle()
{
Obstacle newObstacle;
newObstacle = new Obstacle(counter);
World world;
world = getWorld();
int worldWidth = world.getWidth();
int worldHeight = world.getHeight();
int x = Greenfoot.getRandomNumber(worldWidth);
int y = 1;
if (Greenfoot.getRandomNumber (1000) < 10)
{
world.addObject(newObstacle, x, y);
}
}
}
