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

2023/3/21

How to remove Bullet from world

3
4
5
6
RandomGuy RandomGuy

2023/3/31

#
danpost wrote...
RandomGuy wrote...
It still says identifier expected?
Show the code around the line. Preferably, the code of the entire method that line is in (if not the entire class).
java.lang.IllegalStateException: Actor has been removed from the world. at greenfoot.Actor.failIfNotInWorld(Actor.java:722) at greenfoot.Actor.isTouching(Actor.java:987) at Bullet.collidesWithZombie(Bullet.java:36) at Bullet.act(Bullet.java:26) 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) Caused by: greenfoot.ActorRemovedFromWorld at greenfoot.World.removeObject(World.java:466) at Mythic2.removeObject(Mythic2.java:61) at Bullet.act(Bullet.java:24) ... 4 more This comes up
danpost danpost

2023/3/31

#
RandomGuy wrote...
<< Error Trace Omitted >> This comes up
Please show your current Bullet class codes (entire class). Also, Show your Mythic2 class removeObject method.
RandomGuy RandomGuy

2023/3/31

#
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 Actor
{

    /**
     * Act - do whatever the Bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Bullet()
    {

    }

    public void act() {
        move(6);
        if (isAtEdge() || collidesWithZombie()) {
            getWorld().removeObject(this);
        }
        collidesWithZombie();
        System.out.println("Bullet removed from hitting "+(isAtEdge() ? "edge" : "zombie")+".");
    }

    private void updateHealth()
    {

    }

    private boolean collidesWithZombie() {
        if( ! isTouching(Zombies.class)) return false;
        { Greenfoot.playSound("Gunshot.mp3");
            removeTouching(Zombies.class);
            Counter counter = (Counter) getWorld().getObjects(Counter.class).get(0);
            counter.add(1);
            return true;
        }
    }

    private void repeat()
    {

    }
}
RandomGuy RandomGuy

2023/3/31

#
import greenfoot.*; //(Mythic2,Explorer)  // (World,Explorer, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Mythic2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Mythic2 extends World
{
    private Healthbar healthbar = new Healthbar();
    long lastAdded = System.currentTimeMillis();
    private Counter counter = new Counter();

    /**
     * Constructor for objects of class Mythic2.
     * 
     */
    public Mythic2()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        prepare();
        spawn();
        updateHealth();
    }

    public void updateHealth()
    {
        if (healthbar.getHealth()<=0)
        {
            setBackground("gameover.png");
            Greenfoot.stop();
        }
    }

    private void prepare() 
    {
        Explorer Explorer = new Explorer();
        addObject(Explorer, 86, 206);

    }

    public Healthbar getHealthBar()
    {
        return healthbar;
    }
    public void spawn()
    {

        Zombies Zombies = new Zombies();
        addObject(new Zombies(), 515, 205);
        Counter Counter = new Counter();
        addObject(Counter, 50, 41);
        Healthbar Healthbar = new Healthbar();
        addObject(Healthbar, 530, 33);
    }

    public void removeObject(Actor actor)
    {
        super.removeObject(actor);
        if (actor instanceof Zombies) 
        addObject(new Zombies(), 515, 205);
    }

}
RandomGuy RandomGuy

2023/3/31

#
danpost wrote...
RandomGuy wrote...
<< Error Trace Omitted >> This comes up
Please show your current Bullet class codes (entire class). Also, Show your Mythic2 class removeObject method.
Here you go(above)!
danpost danpost

2023/4/1

#
RandomGuy wrote...
<< Bullet Class Codes Omitted >>
Remove lines 26 and 27 from the Bullet class code (above).
TheGoatFighter TheGoatFighter

2023/4/5

#
The goat wrote...
if(isAtEdge()) { getWorld().removeObject(this); }
There you go, ask help from me, not danpost, i the goat
You need to login to post a reply.
3
4
5
6