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

2023/4/2

I get a terminal window for whatever reason

gijsdevries gijsdevries

2023/4/2

#
i get a terminal window because of the line of code at rule 16. does anyone know how to fix this? when i remove rule 18 it works fine but i need that rule in my code
public class Kogel extends Actor
{
    /**
     * Act - do whatever the Kogel wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        doeKogelDingen();
        vermoordMonster();
        vermoordNachtbaas();
    }
    public void vermoordMonster() {
        Actor monster = getOneIntersectingObject(Monsters.class);
            if (monster != null) {
                updateScoreLevel1();
                getWorld().removeObject(monster);
                getWorld().removeObject(this);
            }
    }
    public void vermoordNachtbaas() {
        Actor nachtbaas = getOneIntersectingObject(Nachtbaas.class);
        if (nachtbaas != null) {
                updateScoreLevel2();
                getWorld().removeObject(this);
            }
    }
    public void doeKogelDingen() {
        move(4);
    }
    private void updateScoreLevel1()
    {
       Level1 level1 = (Level1) getWorld();
       Score score = level1.pakScore(); 
       score.bumpCount(1);
    }
    private void updateScoreLevel2() {
        Level2 level2 = (Level2) getWorld();
        Score2 score2 = level2.pakScore2();
        score2.bumpCount2(1);
    }
}
danpost danpost

2023/4/2

#
gijsdevries wrote...
i get a terminal window because of the line of code at rule 16. does anyone know how to fix this? when i remove rule 18 it works fine but i need that rule in my code << Code Omitted >>
The problem is that when you remove this actor from the world in the vermoordMonster method, then go back to the act method, it then calls the vermoordNachtbaas method, which require the actor still be in the world. Easy fix is to either change line 11 (in act) to:
if (getWorld() != null) vermoordNachtbaas();
OR, insert the following line between lines 10 and 11 (in act):
if (getWorld() == null) return;
You need to login to post a reply.