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

2023/3/20

Error with Zombies

RandomGuy RandomGuy

2023/3/20

#
Hello Guys, I seem to have a problem with my code everytime it is started: It keeps giving me this error: 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.movement(Bullet.java:40) at Bullet.act(Bullet.java:24) 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 Bullet.movement(Bullet.java:36) ... 5 more 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.movement(Bullet.java:40) at Bullet.act(Bullet.java:24) 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 Bullet.movement(Bullet.java:36) It might be to do with my bullet code: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public Bullet() { } public void act() { movement(); } private void movement() { move(6); if (isAtEdge()) { World world = getWorld(); if(world != null) { world.removeObject(this); } } if(isTouching(Zombies.class)) { Counter counter = (Counter) getWorld().getObjects(Counter.class).get(0); counter.add(1); } } } Do you know how I can fix this?
danpost danpost

2023/3/20

#
RandomGuy wrote...
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

    public Bullet()
    {
        

    }

    public void act()
    {
        movement();
    }
    
    private void movement()
    {
        move(6);

        if (isAtEdge())
        {
            World world = getWorld();
            if(world != null)
            {
                world.removeObject(this);
            } 
        }
        
        if(isTouching(Zombies.class))
        {
            Counter counter = (Counter) getWorld().getObjects(Counter.class).get(0);
            counter.add(1);

        }

    }

}
Here is your code with line numbers for referencing.
danpost danpost

2023/3/20

#
RandomGuy wrote...
I seem to have a problem with my code everytime it is started: It keeps giving me this error: << Error Trace Omitted >> It might be to do with my bullet code: << Code Omitted >> Do you know how I can fix this?
Insert the following line immediately after line 23 above so that you do not check if touching zombie when not in the world as line 23 removes the bullet from the world:
return;
RandomGuy RandomGuy

2023/3/20

#
Thanks dude it works! I might need more help so please do check my posts but you are the Greenfoot GOAT
envxity envxity

2023/3/26

#
fr
You need to login to post a reply.