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

2021/6/13

Problem with removeObjects

Turbo_Thorsten Turbo_Thorsten

2021/6/13

#
So I have a problem with methods like removeObjects() or getObjects(). They just don't work and I don't know why.
public void nuke()
    {
     getWorld().removeObjects().getObjects(Bacteria.class); 
    }
Super_Hippo Super_Hippo

2021/6/13

#
Try this:
getWorld().removeObjects(getWorld().getObjects(Bacteria.class));
Turbo_Thorsten Turbo_Thorsten

2021/6/14

#
Super_Hippo wrote...
Try this:
getWorld().removeObjects(getWorld().getObjects(Bacteria.class));
So this works now, but I have another problem. I want to remove my Virus and my Bacteria class objects if nuke() triggers. nuke() can trigger when you kill a virus. But when you do it there is a Null Pointer Exception.
public void nuke()
    {
        if (getWorld().getObjects(Bacteria.class) != null)
        {
     getWorld().removeObjects(getWorld().getObjects(Bacteria.class));
    }
    }
danpost danpost

2021/6/14

#
Just change line 3 to:
if (getWorld() != null)
Turbo_Thorsten Turbo_Thorsten

2021/6/14

#
danpost wrote...
Just change line 3 to:
if (getWorld() != null)
It still won't kill the Bacterias
danpost danpost

2021/6/14

#
Turbo_Thorsten wrote...
It still won't kill the Bacterias
Are you calling the nuke method from act?
Turbo_Thorsten Turbo_Thorsten

2021/6/14

#
danpost wrote...
Turbo_Thorsten wrote...
It still won't kill the Bacterias
Are you calling the nuke method from act?
Full Code (Code from the Bullet):
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bullet extends Mover
{
    /**
     * Act - do whatever the Bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public static double enemieskilled;
    public static int b;
    int c;
    public void act() 
    {
        move (10.0);
        if (isTouching(Bacteria.class))
        {
            //getWorld().addObject(new Explosion(), getX(), getY());
         removeTouching (Bacteria.class);  
         getWorld().removeObject(this);
         enemieskilled++;
        }
        else if (isTouching(Virus.class))
        {
            //getWorld().addObject(new Explosion(), getX(), getY());
         removeTouching (Virus.class);  
         getWorld().removeObject(this);
         enemieskilled++;
         if(Greenfoot.getRandomNumber(100) < 100)
         {
             c = Greenfoot.getRandomNumber(2);
             switch(c)
             {
                 case 0:
                 nuke();
                 break;
                 case 1:
                 Player.fasterShooting();
                 break;
                }
            }
        }
        else if (isAtEdge()) 
        {
         getWorld().removeObject(this);
    }
    }   
    public void nuke()
    {
    if (getWorld() != null)
        {
     getWorld().removeObjects(getWorld().getObjects(Bacteria.class));
    }
    }
    public void nukeScreen()
    {
        
    }
    
}
Normally the nuke only triggers rarely not 50% like in this test version.
Super_Hippo Super_Hippo

2021/6/14

#
You remove the actor from the world in line 32. After that, you call the nuke method (line 40). The actor isn’t in the world anymore, so line 57 will never execute.
danpost danpost

2021/6/14

#
Super_Hippo wrote...
You remove the actor from the world in line 32. After that, you call the nuke method (line 40). The actor isn’t in the world anymore, so line 57 will never execute.
Move line 32 to after line 46.
Turbo_Thorsten Turbo_Thorsten

2021/6/15

#
danpost wrote...
Super_Hippo wrote...
You remove the actor from the world in line 32. After that, you call the nuke method (line 40). The actor isn’t in the world anymore, so line 57 will never execute.
Move line 32 to after line 46.
Thank you it works!
You need to login to post a reply.