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

2016/3/12

How do i make the bullets (Shot) disappear once it reaches the right side of the screen/world?

needhelpp needhelpp

2016/3/12

#
my player (motny) shoots to the right and it could either hit a rock/an alien but if it doesn't hit anything, it would get stuck at the right side of the screen. Could anyone help me solve this problem? What do I need to add/do?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * A bullet to be shot at rocks and aliens
 */
public class Shot  extends Actor
{
    private motny myShip;
    /**
     * Constructor for a Shot. You must specify the ship the shot comes from.
     */
    public Shot(motny myShip)
    {
        this.myShip = myShip;
    }
    /**
     * Act - do whatever the Shot wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        int ypos = getY();
        int xpos=getX();
        if (xpos > 0){
            xpos = xpos + 5;
            setLocation(xpos, ypos);
            Actor rock = getOneIntersectingObject(rock.class);
            Actor alien = getOneIntersectingObject(alien.class);
            if (rock != null) {
                // hits a rock
                getWorld().removeObject(rock);
                getWorld().removeObject(this);
            }
            if (alien != null) {
                //hits an alien
                getWorld().removeObject(alien);
                getWorld().removeObject(this);               
            }
        }
    }
}
Super_Hippo Super_Hippo

2016/3/12

#
At the end, add this:
if (xpos > getWorld().getWidth()-2) getWorld().removeObject(this);
You need to login to post a reply.