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

2023/3/21

How to remove Bullet from world

1
2
3
4
RandomGuy RandomGuy

2023/3/21

#
Hey Guys just a short query is their a specific code to remove my bullet because this is what I did: if(isTouching(Zombies.class)) { World world = getWorld(); world.removeObject(this); }
RandomGuy RandomGuy

2023/3/21

#
That was in the bullet actor anyways
RandomGuy RandomGuy

2023/3/21

#
Please help if you can though
danpost danpost

2023/3/21

#
RandomGuy wrote...
is their a specific code to remove my bullet because this is what I did: << Code Omitted >> That was in the bullet actor anyways
What you did should work provided that the code is executed via the act method in the Bullet class.
RandomGuy RandomGuy

2023/3/21

#
danpost wrote...
RandomGuy wrote...
is their a specific code to remove my bullet because this is what I did: << Code Omitted >> That was in the bullet actor anyways
What you did should work provided that the code is executed via the act method in the Bullet class.
RandomGuy RandomGuy

2023/3/21

#
danpost wrote...
RandomGuy wrote...
is their a specific code to remove my bullet because this is what I did: << Code Omitted >> That was in the bullet actor anyways
What you did should work provided that the code is executed via the act method in the Bullet 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.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:42) at Bullet.act(Bullet.java:23) ... 4 more
RandomGuy RandomGuy

2023/3/21

#
I keep getting this error?
RandomGuy RandomGuy

2023/3/21

#
danpost wrote...
RandomGuy wrote...
is their a specific code to remove my bullet because this is what I did: << Code Omitted >> That was in the bullet actor anyways
What you did should work provided that the code is executed via the act method in the Bullet class.
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() { movement(); if(isTouching(Zombies.class)) { World world = getWorld(); world.removeObject(this); } } private void movement() { move(6); if (isAtEdge()) { World world = getWorld(); if(world != null) { world.removeObject(this); return; } } if(isTouching(Zombies.class)) { Counter counter = (Counter) getWorld().getObjects(Counter.class).get(0); counter.add(1); removeTouching(Zombies.class); GreenfootSound eating = new GreenfootSound("Gunshot.mp3"); if (eating != null) { eating.play(); } } } private void repeat() { } }
RandomGuy RandomGuy

2023/3/21

#
That is my code.Please tell me what I am doing wrong.
danpost danpost

2023/3/22

#
RandomGuy wrote...
I keep getting this error?
Change this line in Bullet class:
if (isTouching(Zombies.class))
to the following:
if (getWorld() != null && isTouching(Zombies.class))
RandomGuy RandomGuy

2023/3/22

#
Ok thanks so much now that the error has gone but the bullet isn't removed when it touches the zombie though?
RandomGuy RandomGuy

2023/3/22

#
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()
    {
        movement();
        if (getWorld() != null && isTouching(Zombies.class))
        {
            World world = getWorld();
            world.removeObject(this);
            
            
        }
    }

    private void movement()
    {
        move(6);

        if (isAtEdge())
        {
            World world = getWorld();
            if(world != null)
            {
                world.removeObject(this);
                return;

            } 
        }
        if (getWorld() != null && isTouching(Zombies.class))
        {
            
            Counter counter = (Counter) getWorld().getObjects(Counter.class).get(0);
            counter.add(1);
            removeTouching(Zombies.class);
            GreenfootSound eating = new GreenfootSound("Gunshot.mp3");
            if (eating != null)
            {
                eating.play();
            }

        }
        
        
    }
    
    private void repeat()
    {
        
    }
}
RandomGuy RandomGuy

2023/3/22

#
Is there anything else?
danpost danpost

2023/3/22

#
RandomGuy wrote...
Is there anything else?
You did not code that in yet.
RandomGuy RandomGuy

2023/3/22

#
Wait so which line should i put the code in? I'm not sure I guess?
There are more replies on the next page.
1
2
3
4