Hello I have a class called Key.class. If all of the Keys have been Collected in the Scenario, a Flag should be spawned. I personally think it is a problem with it spawning infinite Flags/ performing an infinite action, but I don't know how to fix it.
Here is the Code:
And here the crash code:
java.lang.NullPointerException
at Key.Collected(Key.java:19)
at Key.act(Key.java:15)
at greenfoot.core.Simulation.actActor(Simulation.java:567)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:530)
at greenfoot.core.Simulation.runContent(Simulation.java:193)
at greenfoot.core.Simulation.run(Simulation.java:183)
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot und MouseInfo)
/**
* Ergänzen Sie hier eine Beschreibung für die Klasse Key.
*/
public class Key extends Actor
{
int Cyclesleft = 1;
public void act()
{
if(isTouching(Pengu.class)){
getWorld().removeObject (this);
}
Collected();
}
public void Collected ()
{
if (getWorld().getObjects(Key.class).size() == 0){
getWorld().addObject ( new Fahne(), 930, 110 );
}
}
}import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
/**
* This is the whole scene. It creates and contains the objects that are in it.
*/
public class Scene extends World
{
int KeysinWorld = getObjects(Key.class).size();
public Scene()
{
super(1000, 500, 1); // define size and resolution
addObject ( new Pengu(), 66, 304 );
addObject ( new Key(), 100, 100);
}
}
