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

2021/3/20

Spawning Actor not Working

Total-Eclipse Total-Eclipse

2021/3/20

#
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:
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);
        
    }
}
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)
danpost danpost

2021/3/20

#
In Key class, insert the following line at line 13:
return;
(that will prevent throwing of the NullPointerException. The infinite flags issue can be solved by adding an extra condition to spawn a flag (if no flag, add flag). Line 18:
if (getWorld().getObjects(Key.class).isEmpty() && getWorld().getObjects(Flag.class).isEmpty())
Total-Eclipse Total-Eclipse

2021/3/21

#
After inserting the code you sent, it doesn't seem to spawn the Flag, when all Keys have been collected. But the game doesn't crash anymore.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot und MouseInfo)

/**
 * Ergänzen Sie hier eine Beschreibung für die Klasse Key.
 */
public class Key extends Actor
{
    public void act() 
    {
        if(isTouching(Pengu.class)){
            getWorld().removeObject(this);
            return;
        }
        Collected();
    }
    public void Collected()
    {
        if (getWorld().getObjects(Key.class).isEmpty() && getWorld().getObjects(Flag.class).isEmpty()){
            getWorld().addObject( new Flag(), 930, 110);
        }
    }
}
danpost danpost

2021/3/21

#
What you do in Collected is something your World subclass should be doing. Remove line 12 and lines 14 thru 20. Then add the following to the world class:
public void act()
{
    if (.getObjects(Key.class).isEmpty() && .getObjects(Flag.class).isEmpty()){
         addObject( new Flag(), 930, 110);
    }
}
Total-Eclipse Total-Eclipse

2021/3/22

#
I found out that when I set getObjects(Key.class).size(); as a varaible named Keys_in_World, and then inspect the world even though there are 2 KEys visible it shows 0.
danpost danpost

2021/3/22

#
Total-Eclipse wrote...
I found out that when I set getObjects(Key.class).size(); as a varaible named Keys_in_World, and then inspect the world even though there are 2 KEys visible it shows 0.
Show class codes for this.
You need to login to post a reply.