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
danpost danpost

2023/3/22

#
RandomGuy wrote...
Wait so which line should i put the code in?
Line 47 checks for that particular collision; so, anywhere between lines 48 and 59 without further conditions (outside the range 54 to 57).
RandomGuy RandomGuy

2023/3/22

#
I still don't get it. Could you send me the code?
danpost danpost

2023/3/23

#
RandomGuy wrote...
I still don't get it. Could you send me the code?
Make line 58 be:
getWorld().removeObject(this);
RandomGuy RandomGuy

2023/3/23

#
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();
            }
            getWorld().removeObject(this);
        }
        
        
    }
    
    private void repeat()
    {
        
    }
}
RandomGuy RandomGuy

2023/3/23

#
When I tried this it didn't work. What errors are there?
danpost danpost

2023/3/23

#
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.
RandomGuy RandomGuy

2023/3/24

#
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()
    {

    }
}
RandomGuy RandomGuy

2023/3/24

#
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.
I got an error
RandomGuy RandomGuy

2023/3/24

#
also my actor and bullet won't remove so pls send your edited version of my code
envxity envxity

2023/3/24

#
how do you want the actor to move through wasd our automaticallly?
envxity envxity

2023/3/24

#
if you want the user input you would use
private final int speed = 3; // how many pixels the character moves per second
if(Greenfoot.isKeyDown("W")||Greenfoot.isKeyDown("UP")){
setlocation(getX(), getY()-speed);}
if(Greenfoot.isKeyDown("A")||Greenfoot.isKeyDown("LEFT")){
setlocation(getX()-speed, getY());} 
if(Greenfoot.isKeyDown("S")||Greenfoot.isKeyDown("DOWN")){
setlocation(getX(), getY()+speed);}
if(Greenfoot.isKeyDown("D")||Greenfoot.isKeyDown("RIGHT")){
setlocation(getX()+speed, getY());}
envxity envxity

2023/3/24

#
hope that helps
envxity envxity

2023/3/24

#
whered you get the error in the initial segment of code also i could help with that
envxity envxity

2023/3/24

#
RandomGuy wrote...
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.
I got an error
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
envxity envxity

2023/3/24

#
cause im guessing that you are making a similar game to me from youtube by tanner crow
There are more replies on the next page.
1
2
3
4
5