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

2019/5/25

error in the terminal

loldertroll loldertroll

2019/5/25

#
I always get an error message when my crystals break(see code for context) but i dont see the problem. The errors are in line 32 and 16
public class Kristall extends Jim
{
    long berühren = System.currentTimeMillis();
    private int hp=3;
    public void act() 
    {
        fallen();
        touching(); //first error
         
       
    }
        public void fallen()
        {
        move(2);
        setRotation(90);
        if(this.isAtEdge())
        {
            getWorld().removeObject(this);
        }
    } 
    public void touching()
    {
       
         if(isTouching(Geschoss.class)){ //second error
                getWorld().removeObjects(getObjectsInRange(128,Geschoss.class));
                long curTime  = System.currentTimeMillis();
                if (curTime >= berühren + 100){
                    
                
                hp -= 1;
            }
            if(hp<1)
            {
             getWorld().removeObject(this);
            }
        }    
  }
}
Super_Hippo Super_Hippo

2019/5/25

#
The problem is that if the "fallen" method removes the object from the world, the "touching" method tries to use the object's current location in the world. But it isn't in a world anymore. To fix it, you need to prevent the "touching" method from executing if the object isn't in a world anymore.
loldertroll loldertroll

2019/5/25

#
But how
Super_Hippo Super_Hippo

2019/5/25

#
You could add this line to line 23 for example:
if (getWorld() == null) return;
This will exit the method if the object isn't in a world. By the way, if a Kristall isn't a Jim (and it probably isn't), then it shouldn't be a subclass of Jim.
loldertroll loldertroll

2019/5/25

#
ok nvm fixed it. but ty
Super_Hippo Super_Hippo

2019/5/25

#
This should also work:
private int hp = 3;

public Kristall()
{
    setRotation(90);
}

public void act()
{
    move(2);
    if (isTouching(Geschoss.class))
    {
        removeTouching(Geschoss.class);
        if (--hp < 1) getWorld().removeObject(this);
    }
    else if (isAtEdge()) getWorld().removeObject(this);
}
You need to login to post a reply.