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

2023/3/21

How to remove Bullet from world

2
3
4
5
6
RandomGuy RandomGuy

2023/3/29

#
What should I do?
RandomGuy wrote...
RandomGuy wrote...
RandomGuy wrote...
danpost wrote...
RandomGuy wrote...
I don't see anything here(correct me if I am wrong)? Please send me an edited version of my code if possible. It might work?
Remove lines 41 through 54 and change your act method to this:
public void act() {
    move(6);
    if (isAtEdge() || collidesWithZombie()) {
        getWorld().removeObject(this);

Sooy i accidentally wrote my comment in my code:)

Thanks the error is fixed but my bullet isn't removed?
[code]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);
        }
    }

    private boolean collidesWithZombie() {
        
} }
Anything wrong?Bullet doesn't want to remove?
Sorry I wrote my comments in the code.My bullet doesn't disappear when it touches the zombie anyway?
danpost danpost

2023/3/29

#
RandomGuy wrote...
Anything wrong?Bullet doesn't want to remove?
Where is the code to the collidesWithZombie method? The method looks empty.
RandomGuy RandomGuy

2023/3/30

#
danpost wrote...
RandomGuy wrote...
Anything wrong?Bullet doesn't want to remove?
Where is the code to the collidesWithZombie method? The method looks empty.
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);
        }
    }

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

    }
}
Whoops sorry I forgot to copy that
danpost danpost

2023/3/30

#
RandomGuy wrote...
<< Code Omitted >> Whoops sorry I forgot to copy that
If getting errors with this code, show error trace.
RandomGuy RandomGuy

2023/3/30

#
danpost wrote...
RandomGuy wrote...
<< Code Omitted >> Whoops sorry I forgot to copy that
If getting errors with this code, show error trace.
No errors but my bullet won't remove?
danpost danpost

2023/3/30

#
RandomGuy wrote...
No errors but my bullet won't remove?
Insert the following line immediately after (or before) line 24:
System.out.println("Bullet removed from hitting "+(isAtEdge() ? "edge" : "zombie")+".").;
Run scenario and report which removals are being shown (if any).
RandomGuy RandomGuy

2023/3/30

#
danpost wrote...
RandomGuy wrote...
No errors but my bullet won't remove?
Insert the following line immediately after (or before) line 24:
System.out.println("Bullet removed from hitting "+(isAtEdge() ? "edge" : "zombie")+".").;
Run scenario and report which removals are being shown (if any).
I can't put the code in and compile,it says "identifier expected"?
RandomGuy RandomGuy

2023/3/30

#
danpost wrote...
RandomGuy wrote...
No errors but my bullet won't remove?
Insert the following line immediately after (or before) line 24:
System.out.println("Bullet removed from hitting "+(isAtEdge() ? "edge" : "zombie")+".").;
Run scenario and report which removals are being shown (if any).
By the way, thhis project is due for my school assignment in 2 days so please send an edited version,if you can?
danpost danpost

2023/3/30

#
RandomGuy wrote...
I can't put the code in and compile,it says "identifier expected"?
Oops, an extra character slipped into the line. Try this:
System.out.println("Bullet removed from hitting "+(isAtEdge() ? "edge" : "zombie")+".");
RandomGuy RandomGuy

2023/3/31

#
danpost wrote...
RandomGuy wrote...
I can't put the code in and compile,it says "identifier expected"?
Oops, an extra character slipped into the line. Try this:
System.out.println("Bullet removed from hitting "+(isAtEdge() ? "edge" : "zombie")+".");
It still says identifier expected?
Spock47 Spock47

2023/3/31

#
RandomGuy wrote...
No errors but my bullet won't remove?
The source code works for me, so please give more details about the exact situation you are seeing the problem, e.g.: - How do you set up bullet and zombie? What are their positions? What is the width and height of their images? - How do they intersect/touch? - What exactly happens? You say that the "bullet won't remove". Does that mean that the zombie gets removed correctly? - For any error, please copy the whole information Greenfoot gives you for it (especially the line where the error happens). That information is valuable and intended to help with finding the problem. By sharing it, you help the people to help you.
RandomGuy wrote...
It still says identifier expected?
Did you remove the dot before the semicolon like danpost said?
RandomGuy RandomGuy

2023/3/31

#
Spock47 wrote...
RandomGuy wrote...
No errors but my bullet won't remove?
The source code works for me, so please give more details about the exact situation you are seeing the problem, e.g.: - How do you set up bullet and zombie? What are their positions? What is the width and height of their images? - How do they intersect/touch? - What exactly happens? You say that the "bullet won't remove". Does that mean that the zombie gets removed correctly? - For any error, please copy the whole information Greenfoot gives you for it (especially the line where the error happens). That information is valuable and intended to help with finding the problem. By sharing it, you help the people to help you.+ Yep and I cant fit it in
RandomGuy wrote...
It still says identifier expected?
Did you remove the dot before the semicolon like danpost said?
danpost danpost

2023/3/31

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

    }
}
Spock47 Spock47

2023/3/31

#
The statement has to be within the act method, i.e. before the closing brace, to compile. And it should be within the if-block to do what it is supposed to do: Change lines 25 through 28 to:
            System.out.println("Bullet removed from hitting "+(isAtEdge() ? "edge" : "zombie")+".");
        }
    }
There are more replies on the next page.
2
3
4
5
6