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
5
6
envxity envxity

2023/3/24

#
and since you make the counter be based off of a i++ type variable it can go on indefinatly you could probably insert it through for i>= 0 i++{ i++ }
Spock47 Spock47

2023/3/25

#
RandomGuy wrote...
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 (isAtEdge())
        {

            collidesWithZombie(); getWorld().removeObject(this);
        }
    }

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

    private void movement()
    {
        move(6);

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

            } 
        }
        

    }

    private void repeat()
    {

    }
}
I guess you got the error because you still call the movement method (which removes the bullet from the world before the interesting part of the act method runs). Do like danpost said: Replace your act method with
    public void act() {
        move(6);
        if (isAtEdge() || collidesWithZombie()) getWorld().removeObject(this);
    }
If you do that, it should work. If it does not work, post what error occurs or describe what goes wrong. (Note: With the change, the implementation of the movement method is not used anymore, so it can be removed, too.)
RandomGuy RandomGuy

2023/3/25

#
Spock47 wrote...
RandomGuy wrote...
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 (isAtEdge())
        {

            collidesWithZombie(); getWorld().removeObject(this);
        }
    }

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

    private void movement()
    {
        move(6);

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

            } 
        }
        

    }

    private void repeat()
    {

    }
}
I guess you got the error because you still call the movement method (which removes the bullet from the world before the interesting part of the act method runs). Do like danpost said: Replace your act method with
    public void act() {
        move(6);
        if (isAtEdge() || collidesWithZombie()) getWorld().removeObject(this);
    }
If you do that, it should work. If it does not work, post what error occurs or describe what goes wrong. (Note: With the change, the implementation of the movement method is not used anymore, so it can be removed, too.)
So I got the following: java.lang.IllegalStateException: Actor has been removed from the world. at greenfoot.Actor.failIfNotInWorld(Actor.java:722) at greenfoot.Actor.isAtEdge(Actor.java:266) 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 Mythic2.removeObject(Mythic2.java:46) at Bullet.movement(Bullet.java:49) at Bullet.act(Bullet.java:23) ... 4 more java.lang.IllegalStateException: Actor has been removed from the world. at greenfoot.Actor.failIfNotInWorld(Actor.java:722) at greenfoot.Actor.isAtEdge(Actor.java:266) 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 Mythic2.removeObject(Mythic2.java:46) at Bullet.movement(Bullet.java:49) at Bullet.act(Bullet.java:23) ... 4 more
Spock47 Spock47

2023/3/25

#
You have removed the "movement" method, right? Ok, it says that the actor has already been removed. So, two possibilities to deal with it: A. Find out why this happens: Where does the bullet get removed? - Is there any other place (except line 24) in the class Bullet where "getWorld().removeObject(this)" is called, too? - Is there in any other class code to remove a bullet from the world, e.g. "getWorld().removeObject(bullet)"? B. Preventing the source code from being executed when the actor is already gone (without understanding why it happened):
public void act() {
    if (getWorld() == null) return;
    move(6);
    if (isAtEdge() || collidesWithZombie()) getWorld().removeObject(this);
}
I prefer option A (since it means that we also understand why the problem occured), but the choice is yours.
Spock47 Spock47

2023/3/25

#
RandomGuy wrote...
at Bullet.movement(Bullet.java:49) at Bullet.act(Bullet.java:23)
Ok, this shows that the error is because the movement method of Bullet is still called. So either you haven't changed the act method like danpost showed or this is an old error message and the error actually does not occur in new runs anymore (if you haven't cleared the terminal window, old error can still be shown although it will not affect the run of the program). So, just make sure that your act method looks exactly like that (especially the "move(6);" line):
public void act() {
    move(6);
    if (isAtEdge() || collidesWithZombie()) getWorld().removeObject(this);
}
No call to "movement()"!
danpost danpost

2023/3/26

#
RandomGuy wrote...
<< Quote Omitted >> I got an error
What error? Where? Please don't just say " I got an error". That is useless.
danpost danpost

2023/3/26

#
envxity wrote...
<< Quote Omitted >> i think you have to make the counter onto a varriable like i++ and make the boolean public so that way the counter can access it
As far as the boolean being public -- no. The only place the method is called from is in this class; so, private access is fine -- an preferred. The counter does not access it; it accesses the counter by way of the world that the counter is in. As far as the variable itself, if the code given in the original code worked as far as accessing and incrementing the counter, then this particular line is equivalent in accessing the counter and incrementing its variable's value.
danpost danpost

2023/3/26

#
RandomGuy wrote...
So I got the following: << Error Trace Omitted >>
If you replace your entire Bullet class with the entire Bullet class code I provided above, it should work (provided the code for the counter originally given worked).
RandomGuy RandomGuy

2023/3/26

#
danpost wrote...
RandomGuy wrote...
<< Quote Omitted >> I got an error
What error? Where? Please don't just say " I got an error". That is useless.
Sorry I forgot to post the error.It is the one I directed at Spock47
RandomGuy RandomGuy

2023/3/26

#
danpost wrote...
RandomGuy wrote...
When I tried this it didn't work. What errors are there?
Are you saying a bullet striking a zombie is still not being removed? Lines 26 thru 29 can be removed.as it is duplicated code from lines 49, 54 and 60. I would try to consolidate things a bit to make things easier. The isAtEdge method returns a boolean; so, maybe adding a collidesWithZombie method that returns a boolean might help to ease things (since those are the two things that would have the bullet be removed from the world):
import greenfoot.*;

public class Bullet extends Actor
{
    public void act() {
        move(6);
        if (isAtEdge() || collidesWithZombie()) getWorld().removeObject(this);
    }
    
    private boolean collidesWithZombie() {
        if ( ! isTouching(Zombies.class)) return false;
        Greenfoot.playSound("Gunshot.mp3");
        removeTouching(Zombies.class);
        ((Counter)getWorld().getObjects(Counter.class).get(0)).add(1);
        return true;
    }
}
There is not need to check to see if a bullet is in the world anywhere here because it is only removed as the final thing done before exiting the act method. However, I would prefer to have Zombies objects look for Bullet objects and not the other way around as you have it here.
Wait so should I use this code?
danpost danpost

2023/3/26

#
RandomGuy wrote...
Wait so should I use this code?
Did you try it?
RandomGuy RandomGuy

2023/3/26

#
danpost wrote...
RandomGuy wrote...
Wait so should I use this code?
Did you try it?
Yep but the same error comes up?
RandomGuy RandomGuy

2023/3/26

#
I mean the same error I posted a day ago
RandomGuy RandomGuy

2023/3/26

#
So,could you maybe send an edited version of my code. I may not have understood what to do?
danpost danpost

2023/3/27

#
RandomGuy wrote...
same error I posted a day ago
What error did you get with my code? Please copy/paste error trace output here for examination (using my code).
There are more replies on the next page.
1
2
3
4
5
6